min() with custom compare.

Bram Stolk bram at nospam.sara.nl
Wed Apr 7 05:44:07 EDT 2004


Hi there,


If you have a list of elements, you can sort it with an alternative
comparator (default is __lt__())

This is done as:

>>> l=[5,-4,1,9,-9]
>>> l.sort()
>>> l
[-9, -4, 1, 5, 9]
>>> l.sort(lambda x,y: abs(x)-abs(y))
>>> l
[1, -4, 5, -9, 9]

Lovely stuff, is it not?

Now my question:
What if I need only a max, or min value, and not a complete sort,
but I do want to have a custom compare func.
What could I do?

min() and max() built-ins cannot take a compare func.

Thanks,

   Bram

-- 
------------------------------------------------------------------------------
 Bram Stolk, VR Engineer.
 SARA Academic Computing Services Amsterdam, PO Box 94613, 1090 GP  AMSTERDAM
 email: bram at nospam.sara.nl   Phone +31-20-5923059  Fax +31-20-6683167

"For the costs of subsidized agriculture in the EU, we can have all 56 million
 European cows fly around the world. First Class." - J. Norberg
------------------------------------------------------------------------------



More information about the Python-list mailing list