jor*_*mas 2 python tuples list max
I have this list of list of tuples
var1 = [
[(10, '?'), (7, '?')],
[(14, '?'), (2, '?')],
[(2, '?'), (9, '?')],
[(11, '?'), (10, '?')],
[(11, '?'), (5, '?')]
]
Run Code Online (Sandbox Code Playgroud)
and I wanna extract the tuple with the maximun value which is the second one or var1[1]. I've used a lot of different codes during my programming but the one I'm using now and until now and didn't have any major issues is this one:
maximo = max(var1, key=lambda x: sum(i for i,_ in x))
Run Code Online (Sandbox Code Playgroud)
also this one:
maximo2 = list(map(max,zip(*var1)))
Run Code Online (Sandbox Code Playgroud)
问题是我需要具有最大值的列表,而不是具有 2 个最大值组合的列表,现在此代码输出var1[3]为 2 中较大的一个,我不知道还能尝试什么。
使用max代替sum
前任:
var1=[[(10, '?'), (7, '?')], [(14, '?'), (2, '?')], [(2, '?'), (9, '?')], [(11, '?'), (10, '?')], [(11, '?'), (5, '?')]]
maximo=max(var1, key=lambda x: max(i for i,_ in x))
print(maximo)
Run Code Online (Sandbox Code Playgroud)
输出:
[(14, '?'), (2, '?')]
Run Code Online (Sandbox Code Playgroud)