使用OrderedDict格式化问题

时间:2014-11-07 06:38:33

标签: python python-3.x

我希望报告第5列没有重复值。所以我在下面的代码中使用了OrderedDict:

from collections import OrderedDict
with open(notmatch) as infile, open (two, 'w') as outfile:
    reader = csv.reader(infile, delimiter='\t')
    writer = csv.writer(outfile, delimiter='\t')
    for gg, poss, codee, ref, alt, *rest in reader:
        gg = int (gg)
        poss = int(poss)
        cls = ref + alt
        clss = list(OrderedDict.fromkeys(cls))
        writer.writerow([gg, poss, codee, d[gg][poss-1], clss] + rest)

这给了我第5列的输出。“clss”如下:

['A','B','C']
['G','A','T']
['G','A']
['T']

我想要的输出是:

A,B,C
G,A,T
G,A
T

我应该做出哪些改变以获得上述输出(没有括号和撇号)?请帮帮我!

1 个答案:

答案 0 :(得分:1)

我希望这会有所帮助:

for l in [['A','B','C'],['G','A','T'],['G','A'],['T']]:
    print ",".join(l)

输出:

A,B,C                                                                                                          

G,A,T                                                                                                          

G,A                                                                                                            

T  

我相信你的代码看起来像:

for l in [[gg, poss, codee, d[gg][poss-1], clss] + rest]:
    writer.writerow(",".join(l) )