我有一个名称对的元组列表,每对都有一个值,格式如下:
1.[('Carlo', 'Helen', 9), ('Carlo', 'Mary', 4), ('Jan', 'Rolly', 1), ('Renzi', 'Rolly', 3)]
2.[('Sofy', 'Reem', 9), ('Sofy', 'David', 5),('Sofy', 'keem', 3) ('Roly', 'Tony', 4), ('Gianni', 'Tony', 2)]
3.[('Sofy', 'Reem', 9), ('Sofy', 'David', 5),('Sofy', 'keem', 3),('Roly', 'Reem', 5), ('Roly', 'David', 2),('Roly', 'keem', 3)]
我想要的是:对于列表中的每一行,如果两个或多个名称对共享一个名称(第一个或第二个名称),我必须选择具有高值的名称对。因此,对于上面的示例,输出将是
1-('Carlo', 'Helen', 9)
('Renzi', 'Rolly', 3)
2-('Sofy', 'Reem', 9)
('Roly', 'Tony', 4)
有人能帮忙吗?你知道吗
你需要两个功能。一种是迭代分组,另一种是求最大值:
我还没有找到改进的方法,但是你可以把你的清单翻两遍。在外循环中,每一项都被选为候选项。在内部循环中,每个在名字或姓氏上匹配的iteam,当且仅当其得分高于当前候选项时才替换候选项进行选择。在内部循环的末尾,如果候选对象尚未插入,则将其插入到选择列表中。你知道吗
相关问题 更多 >
编程相关推荐