min() with custom compare.

Dan Bishop danb_83 at yahoo.com
Wed Apr 7 10:16:35 EDT 2004


Heather Coppersmith <me at privacy.net> wrote in message news:<m2n05oxdiv.fsf at unique.phony.fqdn>...
> 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?
...
> 
>     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.

minimum = reduce(lambda x, y: min(abs(x), abs(y)), l)



More information about the Python-list mailing list