将列的某些部分移动到另一个python

时间:2015-12-15 16:52:26

标签: python

我有这样的数据:

[(1, 11), (1, 31), (1, 41), (1, 61), (1, 71), (2, 2), (3, 3), (3, 13), (3, 23), (3, 43), (3, 53), (3, 73), (3, 83), (5, 5), (7, 7), (7, 17), (7, 37), (7, 47), (7, 67), (7, 97), (9, 19), (9, 29), (9, 59), (9, 79), (9, 89)]

我正在尝试将其写入此模式的文件中:

(1, 11)   (2, 2)   (3, 3)   (5, 5)   (7, 7)   (9, 19)
(1, 31)            (3, 13)           (7, 17)  (9, 29)
(1, 31)            (3, 23)           (7, 37)  (9, 59)

这是我的代码:

def modulo(inputlist, divider):
    tmplist = []
    for item in inputlist:
        for i in range(divider):
            if item % divider == i:
                tmptuple = (i, item)
                tmplist.append(tmptuple)
    return tmplist  

modulo_list = modulo(a, 10)
modulo_list.sort(key=lambda tup:tup[0])

with open ('modulo_primes.csv', 'w') as out:
    csv_out=csv.writer(out)
    csv_out.writerow(['modulo', 'primary'])
    for row in modulo_list:
        csv_out.writerow(row)

在这种情况下,我使用csv格式,但不需要csv。我只想格式化列中的主数字数据取决于模运算(函数模)。

我的例子是模10的情况,但我计划将其扩展为其他值。 “a”变量是一个简单的素数列表。

1 个答案:

答案 0 :(得分:4)

您可以使用itertools.groupby按第一个元素对元组进行分组,使用itemgetter(0)作为键,itertools.zip_longest将数据转换为列。

l =[(1, 11), (1, 31), (1, 41), (1, 61), (1, 71), (2, 2), (3, 3), (3, 13), (3, 23), (3, 43), (3, 53), (3, 73), (3, 83), (5, 5), (7, 7), (7, 17), (7, 37), (7, 47), (7, 67), (7, 97), (9, 19), (9, 29), (9, 59), (9, 79), (9, 89)]

from itertools import groupby, zip_longest
from operator import itemgetter


with open("out.csv","w") as f:
     r = csv.writer(f,delimiter="\t")
     # itertools.izip_longest for python2
     r.writerows(zip_longest(*(list(v) for k, v in groupby(l, 
                               key=itemgetter(0))),fillvalue="   "))

out.csv:

(1, 11) (2, 2)  (3, 3)  (5, 5)  (7, 7)  (9, 19)
(1, 31)         (3, 13)         (7, 17) (9, 29)
(1, 41)         (3, 23)         (7, 37) (9, 59)
(1, 61)         (3, 43)         (7, 47) (9, 79)
(1, 71)         (3, 53)         (7, 67) (9, 89)
                (3, 73)         (7, 97)     
                (3, 83)                     

如果你想要列对齐,我们需要使用str.format,使用最大元组中的字符数量:

mx = max((len(str(t)) for t in l))
with open("out.csv", "w") as f:
    for row in zip_longest(*(list(v) for k, v in groupby(l, key=itemgetter(0))), fillvalue=" "):
        print(row)
        f.write(" ".join("{:<{mx}}".format(str(t), mx=mx) for t in row)+"\n")

因此输入如下:

l = [(1, 11), (1, 31), (1, 41), (1, 61), (1, 71), (2, 2), (3, 3), (3, 13), (3, 23), (3, 43), (3, 53), (3, 73), (3, 83),
 (5, 5), (7, 7), (7, 17), (7, 3447), (7, 47), (7, 67444), (7, 97), (9, 19), (9, 29), (9, 59), (9, 79), (9, 899)]

你会得到:

(1, 11)    (2, 2)     (3, 3)     (5, 5)     (7, 7)     (9, 19)   
(1, 31)               (3, 13)               (7, 17)    (9, 29)   
(1, 41)               (3, 23)               (7, 3447)  (9, 59)   
(1, 61)               (3, 43)               (7, 47)    (9, 79)   
(1, 71)               (3, 53)               (7, 67444) (9, 899)  
                      (3, 73)               (7, 97)              
                      (3, 83)