I have a Python list contains lots of tuples. I want to find the best two tuples such that which have the best two max range values in it.
list_ = [(55, 55), (77, 81), (95, 129)]
So in this example, I should be able to recover the (77, 81), (95, 129)
. Because 81-77
and 129-95
gives the largest range.
How can I do this in Python?
heapq.nlargest
with a custom key should do the trick:
from heapq import nlargest
list_ = [(55, 55), (77, 81), (95, 129)]
result = nlargest(2, list_, key = lambda x: x[1] - x[0])