Python 如果集合为空则返回布尔值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21191259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning boolean if set is empty
提问by C.B.
I am struggling to find a more clean way of returning a boolean value if my set is empty at the end of my function
如果我的集合在我的函数结束时为空,我正在努力寻找一种更干净的返回布尔值的方法
I take the intersection of two sets, and want to return Trueor Falsebased on if the resulting set is empty.
我取两个集合的交集,并希望返回True或False基于结果集是否为空。
def myfunc(a,b):
c = a.intersection(b)
#...return boolean here
My initial thought was to do
我最初的想法是做
return c is not None
However, in my interpreter I can easily see that statement will return true if c = set([])
但是,在我的解释器中,我可以很容易地看到该语句将返回 true 如果 c = set([])
>>> c = set([])
>>> c is not None
True
I've also tried all of the following:
我还尝试了以下所有方法:
>>> c == None
False
>>> c == False
False
>>> c is None
False
Now I've read from the documentation that I can only use and, or, and notwith empty sets to deduce a boolean value. So far, the only thing I can come up with is returning not not c
现在,我已经从文档阅读,我只能用and,or和not空集来推断一个布尔值。到目前为止,我唯一能想到的就是返回而不是 c
>>> not not c
False
>>> not c
True
I have a feeling there is a much more pythonic way to do this, by I am struggling to find it. I don't want to return the actual set to an if statement because I don't need the values, I just want to know if they intersect.
我有一种感觉,有一种更加 Pythonic 的方法可以做到这一点,因为我正在努力寻找它。我不想将实际集合返回到 if 语句,因为我不需要这些值,我只想知道它们是否相交。
采纳答案by Jonas Sch?fer
回答by Simeon Visser
If cis a set then you can check whether it's empty by doing: return not c.
如果c是一组,那么你可以检查它是否是通过执行空:return not c。
If cis empty then not cwill be True.
如果c为空,那么not c会True。
Otherwise, if ccontains any elements not cwill be False.
否则,如果c包含任何元素not c会False。
回答by jonrsharpe
If you want to return Truefor an empty set, then I think it would be clearer to do:
如果你想要return True一个空集,那么我认为这样做会更清楚:
return c == set()
i.e. "cis equal to an empty set".
即“c等于空set”。
(Or, for the other way around, return c != set()).
(或者,反过来说,return c != set())。
In my opinion, this is more explicit (though less idiomatic) than relying on Python's interpretation of an empty set as Falsein a boolean context.
在我看来,这比False在布尔上下文中依赖 Python 对空集的解释更明确(尽管不那么惯用)。
回答by gefei
not as pythonic as the other answers, but mathematics:
不像其他答案那样pythonic,而是数学:
return len(c) == 0
回答by Andrew Allaire
When you say:
当你说:
c is not None
You are actually checking if c and None reference the same object. That is what the "is" operator does. In python None is a special null value conventionally meaning you don't have a value available. Sorta like null in c or java. Since python internally only assigns one None value using the "is" operator to check if something is None (think null) works, and it has become the popular style. However this does not have to do with the truth value of the set c, it is checking that c actually is a set rather than a null value.
您实际上是在检查 c 和 None 是否引用同一个对象。这就是“is”运算符所做的。在 python 中 None 是一个特殊的空值,通常意味着您没有可用的值。类似于 c 或 java 中的 null。由于python内部仅使用“is”运算符分配一个None值来检查某些内容是否为None(认为null)有效,因此它已成为流行的风格。然而,这与集合 c 的真值无关,它正在检查 c 实际上是一个集合而不是空值。
If you want to check if a set is empty in a conditional statement, it is cast as a boolean in context so you can just say:
如果你想在条件语句中检查一个集合是否为空,它在上下文中被转换为一个布尔值,所以你可以说:
c = set()
if c:
print "it has stuff in it"
else:
print "it is empty"
But if you want it converted to a boolean to be stored away you can simply say:
但是如果你想把它转换成一个布尔值来存储起来,你可以简单地说:
c = set()
c_has_stuff_in_it = bool(c)
回答by Amos Baker
Not as clean as bool(c) but it was an excuse to use ternary.
不像 bool(c) 那样干净,但它是使用三元的借口。
def myfunc(a,b):
return True if a.intersection(b) else False
Also using a bit of the same logic there is no need to assign to c unless you are using it for something else.
同样使用一些相同的逻辑,除非您将它用于其他用途,否则无需分配给 c。
def myfunc(a,b):
return bool(a.intersection(b))
Finally, I would assume you want a True / False value because you are going to perform some sort of boolean test with it. I would recommend skipping the overhead of a function call and definition by simply testing where you need it.
最后,我假设您想要一个 True / False 值,因为您将用它执行某种布尔测试。我建议通过简单地测试你需要的地方来跳过函数调用和定义的开销。
Instead of:
代替:
if (myfunc(a,b)):
# Do something
Maybe this:
也许这个:
if a.intersection(b):
# Do something
回答by shtut shtuzim
"""
This function check if set is empty or not.
>>> c = set([])
>>> set_is_empty(c)
True
:param some_set: set to check if he empty or not.
:return True if empty, False otherwise.
"""
def set_is_empty(some_set):
return some_set == set()

