min() with custom compare.

Felix Wiemann Felix.Wiemann at gmx.net
Wed Apr 7 08:35:13 EDT 2004


Heather Coppersmith schrieb:

> Use reduce:
>
>     def absolulte_minimum_function( x, y ):
>         x, y = abs( x ), abs( y )
>         if x < y:
>             return x
>         else:
>             return y
>
>     minimum = reduce( absolute_minimum_function, l )

That doesn't work if the "minimum" element is negative, e.g.

l = [5, -4, -1, 9, -9].
#           ^^


def absolute_minimum_function(x, y):
    if abs(x) < abs(y):
        return x
    else:
        return y
minimum = reduce(absolute_minimum_function, l)

-- 
http://www.ososo.de/



More information about the Python-list mailing list