Python 从不同列中删除值
您好,我正在尝试编写一个输出文件,如果存在相同的值,则第4列(ref)值将从第5列(alt)中删除 以下是我的代码:Python 从不同列中删除值,python,python-3.x,Python,Python 3.x,您好,我正在尝试编写一个输出文件,如果存在相同的值,则第4列(ref)值将从第5列(alt)中删除 以下是我的代码: with open(two) as infile, open (three, 'w') as outfile: reader = csv.reader(infile, delimiter='\t') writer = csv.writer(outfile, delimiter='\t') for g, pos, code, ref, alt, *rest
with open(two) as infile, open (three, 'w') as outfile:
reader = csv.reader(infile, delimiter='\t')
writer = csv.writer(outfile, delimiter='\t')
for g, pos, code, ref, alt, *rest in reader:
a = alt.split(',')
b = [x for x in a]
if b == ref:
writer.writerow([g, pos, code, ref, [alt-ref]] + rest)
if b != ref:
writer.writerow([g, pos, code, ref, alt] + rest)
我知道[alt-ref]不起作用。我不确定哪种功能可以替代这一部分。
我在第4和第5列中的填充如下所示:
A A,B,C
T H,D,T
H A,H,D,C
以及我想要的输出:
A B,C
T H,D
H A,D,C
有人能帮我吗?非常感谢。您可以使用
set
删除和筛选项目。请检查这个快速示例以实现这一点
注意:我们没有强调如何打开/写入新文件
data= """A A,B,C
T H,D,T
H A,H,D,C"""
newFile=""
for line in data.splitlines(): #Reading the sequence
ref,alt= line.split(" ") #splitting lines, to get ref/alt columns
altList= alt.split(",") #splitting alt to get items
l= list(set(altList)-set(ref)) # delete from alt if the same value is present in ref.
newLine= " ".join([ref,",".join(l)]) #rewriting the data
newFile+=newLine+'\n'
#print newLine
print newFile
输出:
A C,B
T H,D
H A,C,D
你有多个账户吗?我想那是我的账户。如果不是,我可能登录错了gmail。对不起。。