使用python查找不为null的最小值 [英] Find the lowest value that is not null using python
问题描述
我在python中使用了min(A,B,C,D).这在寻找最小值时非常有效,但是,如果我在变量A,B,C或D中都具有空值(0值),它将返回空值(或0).有什么想法在我的情况下如何返回非null或非零值吗?谢谢
I have used min(A,B,C,D) in python. This works perfectly in finding the lowest value, however, if I have a null value (0 value) in any of the varaibles A,B,C or D, it would return the null value (or 0). Any ideas how to return the non-null or non-zero value in my case? thanks
推荐答案
我将使用一个可以处理None,0,False等的过滤器.
I would go with a filter which will handle None, 0, False, etc...
>>> min(filter(None, [1, 2, 0, None, 5, False]))
1
来自文档:
请注意,如果function不为filter,则filter(function,iterable)等效于[如果function(item),则为iterable中的项目的item];如果function为None,则等于[如果item,则为iterable中的item的项目]
Note that filter(function, iterable) is equivalent to [item for item in iterable if function(item)] if function is not None and [item for item in iterable if item] if function is None
这篇关于使用python查找不为null的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!