我想将一些列表分组为一个: 这是我的清单:
myList=[[20, 'Start', '2008-10-10', 'End', '2008-11-09', 'NG'] ,
[21, 'Start', '2008-10-10', 'End', '2008-12-15', 'G'],
[22, 'Start', '2009-11-23', 'End', '2009-12-10', 'B']]
对于开始日期相同的项目,我想用一个列表替换它们,该列表将开始日期和最大结束日期保持在具有相同开始日期和最大结束日期ID的项目中。例如,在上面的示例中,ids 20和21具有相同的开始日期,因此我应该用一个列表替换它们:
[21, 'Start', '2008-10-10', 'End', '2008-12-15', 'G']
因此,此示例的一般输出应如下所示:
myList=[[21, 'Start', '2008-10-10', 'End', '2008-12-15', 'G'],
[22, 'Start', '2009-11-23', 'End', '2009-12-10', 'B']]
答案 0 :(得分:0)
您可以使用itertools.groupby
:
import itertools
import re
myList=[[20, 'Start', '2008-10-10', 'End', '2008-11-09', 'NG'] ,
[21, 'Start', '2008-10-10', 'End', '2008-12-15', 'G'],
[22, 'Start', '2009-11-23', 'End', '2009-12-10', 'B']]
new_list = [[a, list(b)] for a, b in itertools.groupby(sorted(myList, key=lambda x:x[2]), key=lambda x:x[2])]
final_result = [max(b, key=lambda x:list(map(int, re.split('\W+', x[-2])))) for _, b in new_list]
输出:
[[21, 'Start', '2008-10-10', 'End', '2008-12-15', 'G'], [22, 'Start', '2009-11-23', 'End', '2009-12-10', 'B']]
答案 1 :(得分:0)
一种方法可能是试用defaultdict
:
from collections import defaultdict
myDict = defaultdict(list)
for item in myList:
# key as date or third item in list
myDict[item[2]].append(item)
# filter from the result of defaultdict
final_lst = [sorted(v, key = lambda k:k[0], reverse=True)[0] for k, v in list(myDict.items())]
print(final_lst)
结果:
[[21, 'Start', '2008-10-10', 'End', '2008-12-15', 'G'],
[22, 'Start', '2009-11-23', 'End', '2009-12-10', 'B']]