1

我有多个这样的列表:

#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 并选择最低的一个。

[(AAA,['MG_00013','MG_00177','MG_00005'])]

但是,我现在完全迷失和沮丧......

你能帮我解决这个问题吗?

================================================大家帮帮我出来真是太棒了!但是,必须考虑 ID 的长度。例如,每个人都给我带有 MG_00080 的 BBB,但它假设 MG_0045 为 45 小于 80...

4

4 回答 4

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']]

编辑:我不清楚 ids 的比较,但是要以伪数字方式(不是字典顺序)比较它们,替换key=lambda lst: lst[1]

key=lambda lst: int(lst[1][3:])
于 2013-11-03T07:02:49.160 回答
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]
于 2013-11-03T07:08:58.357 回答
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])
于 2013-11-03T07:03:58.830 回答
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'}
于 2013-11-03T07:14:21.880 回答