Python 如何删除逗号分隔单元格中的重复字母

Python 如何删除逗号分隔单元格中的重复字母,python,python-3.x,Python,Python 3.x,嗨,我想删除数据表中单列中的重复条目。在编写输出文件时,有没有一种简单的方法可以做到这一点?这是python3脚本 with open(input) as infile, open (output, 'w') as outfile: reader = csv.reader(infile, delimiter='\t') writer = csv.writer(outfile, delimiter='\t') for gg, poss, codee, ref, alt, *

嗨,我想删除数据表中单列中的重复条目。在编写输出文件时,有没有一种简单的方法可以做到这一点?这是python3脚本

with open(input) as infile, open (output, '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)
        writer.writerow([gg, poss, codee, d[group][poss-1], ref + ',' +alt] + rest)
在最后一行中,我希望列“ref+”、“+alt”不具有任何重复值。使用上述命令,我有如下输出:

A,B,B,C
G,G,A,T
G,A,A
T,T
我希望这是:

A,B,C
G,A,T
G,A
T
我可以在最后一行加入一个简短的命令吗?或者我应该启动一组新的命令来执行此操作?请帮帮我!多谢各位

用OrderedICT编辑

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)
因此,我使用OrderedDict,它似乎为“CLS”列提供了如下输出:


第5列是“ref”和“alt”的concat,这是我想要应用此重复数据消除的唯一列,因此我以这种方式编写了脚本。一切看起来都不错,但每个单元格中都有括号“[”“]和撇号“”。我应该如何修改代码使它们不在那里?

您必须用逗号拆分字符串

>>> a = "A,B,B,C"
>>> a.split(',')
['A', 'B', 'B', 'C']
>>> ','.join(set(a.split(',')))
'A,C,B'
然后使用set获取唯一值

>>> set(a.split(','))
set(['A', 'C', 'B'])
并用逗号再次连接

>>> a = "A,B,B,C"
>>> a.split(',')
['A', 'B', 'B', 'C']
>>> ','.join(set(a.split(',')))
'A,C,B'

A,B,B,C,B的预期输出是什么?是的,顺序很重要。对于A,B,B,C,B,它应该是A,B,C。但是,我的数据集只有1到3个字母。如果顺序很重要,那么我下面的解决方案将不起作用,对不起:@user3546860然后
writer.writerow(list(orderedict.fromkeys(yourlist)))
应该可以。所以我尝试使用OrderedDict,但我有一个小问题。你能看看OP吗?谢谢