For instance:
a=[-5,-3,-1,1,3,5]
I want to find a negative and positive minimum.
Example: negative
print(min(a)) = -5
positive
print(min(a)) = 1
To get the minimum value:
min(a)
To get the minimum positive value:
min(filter(lambda x:x>0,a))
>>> a = [-5,-3,-1,1,3,5] >>> min(el for el in a if el < 0) -5 >>> min(el for el in a if el > 0) 1
Special processing may be required if it adoes not contain any negative or any positive values.
a
Using functools.reduce
>>> from functools import reduce >>> a = [-5,-3,-1,2,3,5] >>> reduce(lambda x,y: x if 0 <= x <=y else y if y>=0 else 0, a) 2 >>> min(a) -5 >>>
Note: This will return 0 if there are no numbers in the list> = 0.