min() with custom compare.

Heather Coppersmith me at privacy.net
Wed Apr 7 06:56:08 EDT 2004


On Wed, 7 Apr 2004 11:44:07 +0200,
Bram Stolk <bram at nospam.sara.nl> wrote:

[ example of sort with custom compare function snipped ]

> 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.

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 )

There's probably some (awfully horrible) lambda-embeddable
equivalent to absolute_minimum_function, but I'm not about to try
to figure it out right now, and even if I did, I'd probably end up
not using it anyway.

HTH,
Heather

-- 
Heather Coppersmith
That's not right; that's not even wrong. -- Wolfgang Pauli



More information about the Python-list mailing list