Issue
I would like to perform the following:
a=max(a,3)
b=min(b,3)
However sometimes a
and b
may be None
.
I was happy to discover that in the case of 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 number in case one of the arguments in None?
Solution
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
Answered By - utdemir
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.