Finding the Min for positive and negative in python 3.3 list

Devin Jeanpierre jeanpierreda at gmail.com
Tue Mar 12 13:49:40 EDT 2013


> min(a)

This does not return a negative minimum on input [1] (because there is none).

> and
>
> min([e for e in a if e >=0]

This does not return a positive minimum on input [0] (because there is none).

I would have said:

    pos_min = min(e for e in a if e > 0)
    neg_min = min(e for e in a if e < 0)

And then deal with the ValueError when there is no such minimum, as appropriate.

-- Devin



More information about the Python-list mailing list