Python 如何检查字典的所有值是否为 0
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35253971/
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
How to check if all values of a dictionary are 0
提问by vishu rathore
I want to check if all the values, i.e values corresponding to all keys in a dictionary are 0. Is there any way to do it without loops? If so how?
我想检查是否所有的值,即对应于字典中所有键的值都是 0。有没有办法在没有循环的情况下做到这一点?如果是这样怎么办?
采纳答案by Shawn
use all()
:
使用all()
:
all(value == 0 for value in your_dict.values())
all
returns True
if all elements of the given iterable are true.
all
True
如果给定可迭代对象的所有元素都为真,则返回。
回答by Fanchi
You can use the any()method, basically it checks for boolean parameters, but 0 will act as False in this case, and any other number as True. Try this code PY2:
您可以使用any()方法,基本上它检查布尔参数,但在这种情况下 0 将作为 False,而任何其他数字作为 True。试试这个代码 PY2:
dict1 = {"a": 0, "b": 1}
dict2 = {"a": 0, "b": 0}
print not any(dict1.itervalues())
print not any(dict2.itervalues())
PY3: dict1 = {"a": 0, "b": 1} dict2 = {"a": 0, "b": 0}
PY3: dict1 = {"a": 0, "b": 1} dict2 = {"a": 0, "b": 0}
print(not any(dict1.values()))
print(not any(dict2.values()))
Output:
输出:
False
True
Edit2: one sidenote/caution, calling any() with an empty list of elements will return False. Edit3: Thanks for comments, updated the code to reflect python 3 changes to dictionary iteration and print function.
编辑 2:一个旁注/警告,使用空元素列表调用 any() 将返回 False。Edit3:感谢您的评论,更新了代码以反映 Python 3 对字典迭代和打印功能的更改。
回答by timgeb
With all
:
与all
:
>>> d = {1:0, 2:0, 3:1}
>>> all(x==0 for x in d.values())
False
>>> d[3] = 0
>>> all(x==0 for x in d.values())
True
No matter whether you use any
or all
, the evaluation will be lazy. all
returns False
on the first falsy value it encounters. any
returns True
on the first truthy value it encounters.
不管你用any
还是all
,评估都会偷懒。all
返回False
它遇到的第一个假值。any
返回True
它遇到的第一个真值。
Thus, not any(d.values())
will give you the same result for the example dictionary I provided. It is a little shorter than the all
version with the generator comprehension. Personally, I still like the all
variant better because it expresses what you want without the reader having to do the logical negation in his head.
因此,not any(d.values())
对于我提供的示例词典,将为您提供相同的结果。它比all
带有生成器理解的版本短一点。就个人而言,我仍然更喜欢这个all
变体,因为它表达了你想要的东西,而无需读者在头脑中进行逻辑否定。
There's one more problem with using any
here, though:
any
但是,在这里使用还有一个问题:
>>> d = {1:[], 2:{}, 3:''}
>>> not any(d.values())
True
The dictionary does not contain the value 0, but not any(d.values())
will return True
because all values are falsy, i.e. bool(value)
returns False
for an empty list, dictionary or string.
字典不包含值 0,但not any(d.values())
会返回,True
因为所有值都是假的,即bool(value)
返回False
空列表、字典或字符串。
In summary: readability counts, be explicit, use the all
solution.
总之:可读性很重要,要明确,使用all
解决方案。
回答by the_unknown_spirit
You may also do it the other way using any :
您也可以使用 any 以其他方式执行此操作:
>>> any(x != 0 for x in somedict.values())
If it returns True , then all the keys are not 0 , else all keys are 0
如果它返回 True ,那么所有的键都不是 0 ,否则所有的键都是 0