I would like to perform the following:
a=max(a,3)
b=min(b,3)
However sometimes a
and b
may be None
.
max
it works out nicely, giving my required result 3
, however if b
is None
, b
remains None
...
Anyone can think of an elegant little trick to make min
return the num开发者_如何学Gober in case one of the arguments in None?
Why don't you just create a generator without None values? It's simplier and cleaner.
>>> l=[None ,3]
>>> min(i for i in l if i is not None)
3
My solution for Python 3 (3.4 and greater):
min((x for x in lst if x is not None), default=None)
max((x for x in lst if x is not None), default=None)
A solution for the Python 3
Code:
# variable lst is your sequence
min(filter(lambda x: x is not None, lst)) if any(lst) else None
Examples:
In [3]: lst = [None, 1, None]
In [4]: min(filter(lambda x: x is not None, lst)) if any(lst) else None
Out[4]: 1
In [5]: lst = [-4, None, 11]
In [6]: min(filter(lambda x: x is not None, lst)) if any(lst) else None
Out[6]: -4
In [7]: lst = [0, 7, -79]
In [8]: min(filter(lambda x: x is not None, lst)) if any(lst) else None
Out[8]: -79
In [9]: lst = [None, None, None]
In [10]: min(filter(lambda x: x is not None, lst)) if any(lst) else None
In [11]: print(min(filter(lambda x: x is not None, lst)) if any(lst) else None)
None
Notes:
Worked in sequence presents as numbers as well as None. If all values is None min() raise exception
ValueError: min() arg is an empty sequence
This code resolve this problem at all
Pros:
- Worked if None presents in sequence
- Worked on Python 3
- max() will be work also
Cons
- Need more than one non-zero variable in the list. i.e. [0,None] fails.
- Need a variable (example lst) or need duplicate the sequence
Here is a decorator that you can use to filter out None values that might be passed to a function:
def no_nones(fn):
def _inner(*args):
return fn(a for a in args if a is not None)
return _inner
print no_nones(min)(None, 3)
print no_nones(max)(None, 3)
prints:
3
3
def max_none(a, b):
if a is None:
a = float('-inf')
if b is None:
b = float('-inf')
return max(a, b)
def min_none(a, b):
if a is None:
a = float('inf')
if b is None:
b = float('inf')
return min(a, b)
max_none(None, 3)
max_none(3, None)
min_none(None, 3)
min_none(3, None)
You can use an inline if
and an infinity as the default, as that will work for any value:
a = max(a if a is not None else float('-inf'), 3)
b = min(b if b is not None else float('inf'), 3)
a=max(a,3) if a is not None else 3
b=min(b,3) if b is not None else 3
@utdemir's answer works great for the provided example but would raise an error in some scenarios.
One issue that comes up is if you have a list with only None
values. If you provide an empty sequence to min()
, it will raise an error:
>>> mylist = [None, None]
>>> min(value for value in mylist if value)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: min() arg is an empty sequence
As such, this snippet would prevent the error:
def find_minimum(minimums):
potential_mins = (value for value in minimums if value is not None)
if potential_mins:
return min(potential_mins)
I believe the cleanest way is to use filter built-in function
a = max(filter(None, [a, 3]))
b = min(filter(None, [b, 3]))
If you can use np.nan instead of None:
import numpy as np
float(np.nanmax([a, np.nan]))
float(np.nanmin([a, np.nan]))
Based on @PADYMKO's answer you can also use numpy array .any()
to filter zeros from None
if your array contains both :
>>> lst = np.asarray(lst)
>>> min(filter(lambda x: x is not None, lst)) if (lst != None).any() else None
Results :
>>> lst = [None, None]
>>> lst= np.array(lst)
>>> min(filter(lambda x: x is not None, lst)) if (lst != None).any() else None
>>> lst = [0, None]
>>> lst= np.array(lst)
>>> min(filter(lambda x: x is not None, lst)) if (lst != None).any() else None
0
>>> lst = [0, -5, 2, None]
>>> lst= np.array(lst)
>>> min(filter(lambda x: x is not None, lst)) if (lst != None).any() else None
-5
精彩评论