max() of a list of tuples
Scott David Daniels
Scott.Daniels at Acm.Org
Tue Jan 21 17:06:14 EST 2003
I know this isn't clever enough, but:
source = [(1,3,5), (8,16,2), (2,56,4)] # or whatever
best = source[0]
for element in source:
if best[-1] < element[-1]:
best = element
Leaving the answer in best.
or perhaps you'd prefer:
score = min([element[-1] for element in source])
for best in source:
if score == best[-1]:
break
or even:
if source:
score = min([element[-1] for element in source])
bestlist = [element[-1] for element in source
if score == element[-1]]
else:
bestlist = []
# Which gives all elements with the highest score.
Variables named "i" always look like integers to me.
-Scott David Daniels
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list