max() of a list of tuples

Jp Calderone exarkun at intarweb.us
Tue Jan 21 17:09:16 CET 2003


On Tue, Jan 21, 2003 at 11:58:14AM +0200, Mario Wehbrink wrote:
> Hi,
> 
> i have a list of tuples that look like:
> [(1,3,5), (8,16,2), (2,56,4)]
> 
> what i am interested, in is the tuple with the greatest value in pos 3.
> So in this case it would be (1,3,5). Is there a way to tell
> max(mylistoftuples) to only look at the last position of the tuples?
> 

  Here's another way:

    L = [(1,3,5), (8,16,2), (2,56,4)]
    s = L[:]
    s.sort(lambda x, y: cmp(x[2], y[2]))
    print s[-1]

  Jp

-- 
"There is no reason for any individual to have a computer in their
home."
                -- Ken Olson, President of DEC, World Future Society
                   Convention, 1977
--
 12:00am up 36 days, 9:48, 4 users, load average: 0.10, 0.14, 0.14
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030121/0a9c6c52/attachment.pgp>


More information about the Python-list mailing list