根据列表中的以下元素获取第一个元素的范围

时间:2017-11-20 09:30:09

标签: python list itertools

我正在努力解决以下问题。基本上我有一个清单:

dolist = [(1280, ['A1'], ['A2']), (1278, ['A1'], ['A2']), (1276, ['A1'], ['A2']), (1274, ['B1'], ['B2']), (1272, ['A1'], ['A2']), (1270, [], ['A2'])]

现在我希望列出按元素2和3排序的列表。

uniqdo = [ (['A1'],['A2']), (['B1'],['B2']),([],['A2']) ]
dorange = [ "1280-1276,1272","1274","1270" ]

我尝试过简单的比较,但代码变得很长,有几个测试,看起来有点乱。必须有库函数才能合理地快速完成。

1 个答案:

答案 0 :(得分:1)

看起来itertools.groupby可以帮到你:

>>> dolist = [ (1280,['A1'],['A2']),(1278,['A1'],['A2']),(1276,['A1'],['A2']),(1274,['B1'],['B2']),(1272,['A1'],['A2']) ]
>>> from itertools import groupby
>>> [[v, [i for i,*_ in g]] for v, g in groupby(dolist, key= lambda l: (l[1][0], l[2][0]))]
[[('A1', 'A2'), [1280, 1278, 1276]], [('B1', 'B2'), [1274]], [('A1', 'A2'), [1272]]]

将上述数据结构转换为您想要的数据结构并不困难。

这是一个开始。您不能将任何列表保留为输入,因为Python列表不能用作dict键。因此get_value会返回None而不是空列表:

from itertools import groupby

dolist = [(1280, ['A1'], ['A2']), (1278, ['A1'], ['A2']), (1276, ['A1'], ['A2']), (1274, ['B1'], ['B2']), (1272, ['A1'], ['A2']), (1270, [], ['A2'])]
ranges = {}


def get_value(l):
    if l:
        return l[0]
    else:
        return None


def get_values(t):
    return (get_value(t[1]), get_value(t[2]))

for v, g in groupby(dolist, get_values):
    ids = [str(t[0]) for t in g]
    if len(ids) > 1:
        range_str = ids[0] + '-' + ids[-1]
    else:
        range_str = ids[0]
    ranges.setdefault(v, []).append(range_str)

print(ranges)
# {('A1', 'A2'): ['1280-1276', '1272'], ('B1', 'B2'): ['1274'], (None, 'A2'): ['1270']}