from operator import itemgetter
lst = [[A, teamAScore], [B, teamBScore], [C, teamCScore], [D, teamDScore]]
# get max of list based on second element of each sublist i.e teamxScore
mx = max(lst,key=litemgetter(1)))
# use a list comp to find all sublists where teamxScore is equal to the max
maxes = [ele for ele in lst if ele[1] == mx[1]]
演示:
l = [["foo", 2], ["bar", 1], ["foobar", 2]]
mx = max(l, key=itemgetter(1))
maxes = [ele for ele in l if ele[1] == mx[1]]
我假设你想要基于分数的最大值,即第二个元素,因此首先根据每个子列表分数的第二个元素得到最大值,然后保持所有分数等于最大值的子列表:
演示:
输出:
foo和foobar的得分都等于max,所以我们返回了两个子列表。你知道吗
相关问题 更多 >
编程相关推荐