例如,在指定具有第一优先级的列表之后,我需要为每个唯一名称选择一个最小值的列表
原始嵌套列表:
lst=[[['ahmad','a',5],['ahmad','a',6],['ahmad','c',4],['Emme','b',5],['Emme','b',4]],[['ahmad','b',5],['ahmad','b',6],['ahmad','c',6],['ahmad','c',5],['Meno','c',4],['Emme','b',5],['Moo','b',4],['Moo','a',7],['Moo','a',5]]]
每个列表表示为:['name', 'priority term', value]
。你知道吗
优先级依次为“a”、“b”和“c”。你知道吗
预期结果:
new_lst=[[['ahmad','a',5],['Emme','b',4]],[['ahmad','b',5],['Meno','c',4],['Emme','b',5],['Moo','a',5]]]
更新:
如果列表:
lst=[[['ahmad','red',5,20,'a'],['ahmad','red',6,21,'a'],['ahmad','blue',4,15,'c'],['Emme','red',5,30,'b'],['Emme','red',4,12,'b']],[['ahmad','blue',5,10,'b'],['ahmad','blue',6,13,'b'],['ahmad','blue',6,15,'c'],['ahmad','blue',5,30,'c'],['Meno','green',4,40,'c'],['Emme','green',5,35,'b'],['Moo','red',4,7,'b'],['Moo','red',7,3,'a'],['Moo','red',5,18,'a']]]
每个列表都表示为:['name','color',value, trivial number, 'priority term']
。你知道吗
预期结果:
new_list=[[['ahmad','red',5,20,'a'],['ahmad','blue',4,15,'c'],['Emme','red',4,12,'b']],[['ahmad','blue',5,10,'b'],['Meno','green',4,40,'c'],['Emme','green',5,35,'b'],['Moo','red',5,18,'a']]]
你可以使用字典来保持你的优先顺序。然后使用
sorted
后跟toolz.unique
对重复名称进行排序和删除:如果您没有访问第三方^{} 的权限,请注意该函数与
itertools
unique_everseen
recipe相同。你知道吗相关问题 更多 >
编程相关推荐