Python 检查 iterable 的所有值是否为零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3525953/
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
Check if all values of iterable are zero
提问by mjschultz
Is there a good, succinct/built-in way to see if all the values in an iterable are zeros? Right now I am using all()with a little list comprehension, but (to me) it seems like there should be a more expressive method. I'd view this as somewhat equivalent to a memcmp()in C.
有没有一种好的、简洁的/内置的方法来查看迭代中的所有值是否为零?现在我正在使用all()一些列表理解,但是(对我而言)似乎应该有一种更具表现力的方法。我认为这在某种程度上相当于memcmp()C 中的a 。
values = (0, 0, 0, 0, 0)
# Test if all items in values tuple are zero
if all([ v == 0 for v in values ]) :
print 'indeed they are'
I would expect a built-in function that does something like:
我希望有一个内置函数可以执行以下操作:
def allcmp(iter, value) :
for item in iter :
if item != value :
return False
return True
Does that function exist in python and I'm just blind, or should I just stick with my original version?
该功能是否存在于python中而我只是瞎了眼,还是应该坚持使用原始版本?
Update
更新
I'm not suggesting that allcmp()is the solution. It is an example of what I thinkmight be more meaningful. This isn't the place where I would suggest new built-ins for Python.
我并不是建议这allcmp()是解决方案。这是我认为可能更有意义的一个例子。这不是我建议 Python 新内置函数的地方。
In my opinion, all()isn't that meaningful. It doesn't express what "all" is checking for. You could assume that all()takes an iterable, but it doesn't express what the function is looking for (an iterable of bools that tests all of them for True). What I'm asking for is some function likemy allcmp()that takes two parameters: an iterable and a comparison value. I'm asking if there is a built-in function that does something similar to my made up allcmp().
在我看来,all()是不是很有意义。它没有表达“所有”正在检查的内容。你可以假设它all()需要一个可迭代的,但它没有表达函数正在寻找什么(一个bools的可迭代的测试所有的 s True)。我要的是一些像我allcmp()这样的函数,它有两个参数:一个可迭代值和一个比较值。我在问是否有一个内置函数可以执行类似于我的 make up 的功能allcmp()。
I called mine allcmp()because of my C background and memcmp(), the name of my made up function is irrelevanthere.
我打电话给我的allcmp(),因为我的C背景memcmp(),我由函数的名称是无关紧要的位置。
采纳答案by Tomasz Wysocki
Use generators rather than lists in cases like that:
在这种情况下使用生成器而不是列表:
all(v == 0 for v in values)
Edit:
编辑:
allis standard Python built-in. If you want to be efficient Python programmer you should know probably more than half of them (http://docs.python.org/library/functions.html). Arguing that alltrueis better name than allis like arguing that C whileshould be call whiletrue. Is subjective, but i think that most of the people prefer shorter names for built-ins. This is because you should know what they do anyway, and you have to type them a lot.
all是标准的 Python 内置。如果你想成为高效的 Python 程序员,你应该知道其中的一半以上(http://docs.python.org/library/functions.html)。争论这个alltrue名字比all争论 Cwhile应该是 call更好whiletrue。是主观的,但我认为大多数人更喜欢内置函数的较短名称。这是因为无论如何你都应该知道它们是做什么的,而且你必须大量输入它们。
Most of Python programmers will understand code I wrote in seconds. I will probably test this code on my girlfried (she is architect, not a programmer, never used Python) and I think she will get it. Using generators is better than using numpy because generators have more elegant syntax. numpy may be faster, but you will benefit only in rare cases (generators like I showed are fast, you will benefit only if this code is bottleneck in your program).
大多数 Python 程序员会在几秒钟内理解我编写的代码。我可能会在我的女朋友身上测试这段代码(她是架构师,不是程序员,从未使用过 Python),我想她会明白的。使用生成器比使用 numpy 更好,因为生成器有更优雅的语法。numpy 可能更快,但您只会在极少数情况下受益(我展示的生成器速度很快,只有当此代码成为程序中的瓶颈时,您才会受益)。
You probably can't expect nothing more descriptive from Python.
您可能无法期望 Python 提供更多的描述性内容。
PS. Here is code if you do this in memcpm style (I like all version more, but maybe you will like this one):
附注。如果您以 memcpm 风格执行此操作,这里是代码(我更喜欢所有版本,但也许您会喜欢这个):
list(l) == [0] * len(l)
回答by Mark Byers
If you know that the iterable will contain only integers then you can just do this:
如果您知道可迭代对象仅包含整数,那么您可以这样做:
if not any(values):
# etc...
回答by John La Rooy
If values is a numpy array you can write
如果 values 是一个 numpy 数组,你可以写
import numpy as np
values = np.array((0, 0, 0, 0, 0))
all(values == 0)
回答by Bloop93
The built-in setis given an iterable and returns a collection (set) of unique values.
内置set函数被赋予一个可迭代对象并返回一个唯一值的集合(集合)。
So it can be used here as:
所以它可以在这里用作:
set(it) == {0}
- assuming
itis the iterable {0}is a set containing only zero
- 假设
it是可迭代的 {0}是一个只包含零的集合
More info on python set-types-set-frozenset herein docs.
有关python更多信息设置类型,设置frozenset这里的文档。
回答by Harry
The any() function may be the most simple and easy way to achieve just that. If the iterable is empty,e.g. all elements are zero, it will return False.
any() 函数可能是实现这一目标的最简单易行的方法。如果可迭代对象为空,例如所有元素都为零,它将返回 False。
values = (0, 0, 0, 0, 0)
print (any(values)) # return False

