我有多个这样的列表:
#Symbol ID
['AAA','MG_00013']
['AAA','MG_00177']
['AAA','MG_00005']
['BBB','MG_0045']
['BBB','MG_00080']
['CCC','MG_0002'] # and so on...
我想选择具有最小ID的相同符号的列表。
所以,最终的结果是这样的:
#Symbol ID
['AAA','MG_00005']
['BBB','MG_0045']
['CCC','MG_0002'] #...
为此,我将它们列入了列表
listoflists =[['AAA','MG_00013'],['AAA','MG_00177'],['AAA','MG_00005'],['BBB','MG_0045'],['BBB','MG_00080'],['CCC','MG_0002']]
我从这里迷路了......
for i in listoflists:
if i[0] == i[0]:
test.append(i[1])
for i in test:
print(i)
给出了错误的结果。
我认为逻辑是将它们变成如下所示的列表并比较字母数字ID并选择最低的ID。
[(AAA,['MG_00013','MG_00177','MG_00005'])]
然而,我现在完全失去了沮丧......
请你帮助我解决这个问题吗?
=============================================== 每个帮助我的人都是如此伟大! 但是,必须考虑ID的长度。 例如,每个人都给我BBB与MG_00080,但它假设MG_0045为45小于80 ...
答案 0 :(得分:4)
我认为像字典这样的东西可能会更好,但这会给你预期的输出。
import itertools
listoflists =[['AAA','MG_00013'],['AAA','MG_00177'],['AAA','MG_00005'],['BBB','MG_0045'],['BBB','MG_00080'],['CCC','MG_0002']]
minlists = [
min(value, key=lambda lst: lst[1])
for _, value in itertools.groupby(listoflists, lambda lst: lst[0])
]
print minlists
输出
[['AAA', 'MG_00005'], ['BBB', 'MG_00080'], ['CCC', 'MG_0002']]
编辑:对我的比较并不清楚,但是要用伪数字(不是词汇表)比较它们,用
替换key=lambda lst: lst[1]
key=lambda lst: int(lst[1][3:])
答案 1 :(得分:2)
这是defaultdict
from collections import defaultdict
D = defaultdict(list)
for k,v in listoflists:
D[k].append(v)
return [[k, min(D[k])] for k in D]
答案 2 :(得分:1)
您可以将其转换为列表词典
d = { k[0] : [] for k in listoflists }
for k in listoflists: d[k[0]].append(k[1])
ans = [ [k,min(d[k])] for k in d ]
print ans
或只是
d = { k[0] : [] for k in listoflists }
for k in listoflists: d[k[0]].append(k[1])
for k in d: print k,min(d[k])
答案 3 :(得分:1)
ll =[['AAA','MG_00013'],
['AAA','MG_00177'],
['AAA','MG_00005'],
['BBB','MG_0045'],
['BBB','MG_00080'],
['CCC','MG_0002']]
d = {}
for l in ll:
# If key is not the dict, insert the entry into dict
if l[0] not in d:
d[l[0]] = l[1]
# If key is already in the dict, update the entry if value is smaller
elif int(l[1][3:]) < int(d[l[0]][3:]):
d[l[0]] = l[1]
print d
输出:
{'AAA': 'MG_00005', 'BBB': 'MG_0045', 'CCC': 'MG_0002'}