Finding the Min for positive and negative in python 3.3 list

Terry Reedy tjreedy at udel.edu
Tue Mar 12 13:57:08 EDT 2013


On 3/12/2013 1:03 PM, Norah Jones wrote:
> For example:
> a=[-15,-30,-10,1,3,5]
>
> I want to find a negative and a positive minimum.
>
> example: negative
> print(min(a)) = -30
>
> positive
> print(min(a)) = 1

If this is homework, stop reading and do it yourself ;-)
Otherwise...
 >>> min(i for i in a if i >0)
1
 >>> max(i for i in a if i < 0)
-10
 >>> min(i for i in a if i < 0)
-30

You did not specify if you would include 0 in a pos min (0 is neither 
really positive or negative).

-- 
Terry Jan Reedy




More information about the Python-list mailing list