我想在第二列中保留唯一整数,并在上一个(第一个)列中打印该条目。但我无法将字符串转换为整数并得到错误: TypeError:'int'对象不可迭代
输入文件
A 1 108.80
A 1 8.33
B 2 45.10
B 2 3.96
B 2 3.94
A 3 1.96
A 3 2.94
输出
A
B
A
脚本
with open('test.pdb') as infile:
for line in infile:
data = int(line.split()[1])
seen = set()
for number in range(data):
unique = [x for x in number if x not in seen]
seen.update(unique)
print(unique)
答案 0 :(得分:3)
这是你(可能)想要的:
seen = set()
with open('test.pdb') as infile:
for line in infile:
unique, data = line.split()[0:2]
if data not in seen:
print(unique)
seen.add(data)
答案 1 :(得分:1)
鉴于这个例子,这里有一个列表理解来处理:
with open(fn) as f:
seen=set()
print '\n'.join([c1 for c1,c2 in (line.split()[0:2] for line in f)
if not (c2 in seen or seen.add(c2))])
打印:
A
B
A