This works:
>>> a_list = [(20, 1), (16, 0), (21, 0), (20, 0), (24, 0), (25, 1)]
>>> a = min(x[1] for x in a_list)
>>> max(b for b in a_list if b[1] == a)
(24, 0)
>>>
Question
I have a list of tuples with 2 integers each:
a_list=[(20, 1), (16, 0), (21, 0), (20, 0), (24, 0), (25, 1)]
What I am looking for is a way to find and return the tuple that has the smallest second item with the biggest first item. In the above list, it will be the tuple (24, 0)
.
La solution 2
This works:
>>> a_list = [(20, 1), (16, 0), (21, 0), (20, 0), (24, 0), (25, 1)]
>>> a = min(x[1] for x in a_list)
>>> max(b for b in a_list if b[1] == a)
(24, 0)
>>>
Autres conseils
Your example is trivial enough for the following approach to work
>>> 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)
It assumes the fact that
Tuples are compared lexicographic-ally based on indexes
See 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
Ordering of numbers revereses on change of sign
Assuming that my understanding is correct, then here's what you need to do.
Find the minimum second value:
lowest = min(a_list, key=lambda t: t[1])[1]
Then find all the items that have that as the second item
having_lowest = filter(lambda t: t[1] == lowest, a_list)
Then find the one with the highest first number
having_max = max(having_lowest, key=lambda t: t[0])