我有一个元组列表,每个元组有 2 个整数:
a_list=[(20, 1), (16, 0), (21, 0), (20, 0), (24, 0), (25, 1)]
我正在寻找的是一种查找并返回具有最小第二项和最大第一项的元组的方法。在上面的列表中,它将是元组 (24, 0)
。
您的示例对于以下方法来说足够简单
>>> a_list=[(20, 1), (16, 0), (21, 0), (20, 0), (24, 0), (25, 1)]
>>> max(a_list, key=lambda e:(-e[-1],e[0]))
(24, 0)
假设
元组根据索引进行字典顺序比较
参见 doc :
Sequence types also support comparisons. In particular, tuples and lists are compared lexicographically by comparing corresponding elements. This means that to compare equal, every element must compare equal and the two sequences must be of the same type and have the same length
数字的顺序随着符号的改变而反转
我是一名优秀的程序员,十分优秀!