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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:12:01  来源:igfitidea点击:

How to check if all values of a dictionary are 0

pythondictionary

提问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())

allreturns Trueif all elements of the given iterable are true.

allTrue如果给定可迭代对象的所有元素都为真,则返回。

回答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 anyor all, the evaluation will be lazy. allreturns Falseon the first falsy value it encounters. anyreturns Trueon 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 allversion with the generator comprehension. Personally, I still like the allvariant 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 anyhere, 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 Truebecause all values are falsy, i.e. bool(value)returns Falsefor an empty list, dictionary or string.

字典不包含值 0,但not any(d.values())会返回,True因为所有值都是假的,即bool(value)返回False空列表、字典或字符串。

In summary: readability counts, be explicit, use the allsolution.

总之:可读性很重要,要明确,使用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