Python - 检查多个变量是否具有相同的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37376516/
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
Python - Check if multiple variables have the same value
提问by Nevermore
I have a set of three variables x, y, z and I want to check if they all share the same value. In my case, the value will either by 1 or 0, but I only need to know if they are all the same. Currently I'm using
我有一组三个变量 x、y、z,我想检查它们是否都共享相同的值。在我的情况下,该值将是 1 或 0,但我只需要知道它们是否都相同。目前我正在使用
if 1 == x and 1 == y and 1 == z:
sameness = True
Looking for the answer I've found:
寻找我找到的答案:
if 1 in {x, y, z}:
However, this operates as
然而,这作为
if 1 == x or 1 == y or 1 == z:
atleastOneMatch = True
Is it possible to check if 1 is in each: x, y, and z? Better yet, is there a more concise way of checking simply if x, y, and z are the same value?
是否可以检查 1 是否在每个:x、y 和 z 中?更好的是,是否有更简洁的方法来简单地检查 x、y 和 z 是否相同?
(if it matters, I use python 3)
(如果重要,我使用python 3)
回答by Martijn Pieters
If you have an arbitrary sequence, use the all()
functionwith a generator expression:
values = [x, y, z] # can contain any number of values
if all(v == 1 for v in values):
otherwise, just use ==
on all three variables:
否则,只需==
在所有三个变量上使用:
if x == y == z == 1:
If you only needed to know if they are all the same value (regardless of what value that is), use:
如果您只需要知道它们是否都是相同的值(无论是什么值),请使用:
if all(v == values[0] for v in values):
or
或者
if x == y == z:
回答by Neapolitan
To check if they are all the same (either 1 or 2):
要检查它们是否都相同(1 或 2):
sameness = (x == y == z)
The parentheses are optional, but I find it improves readability
括号是可选的,但我发现它提高了可读性
回答by zondo
You could use something similar to what you have:
你可以使用类似于你所拥有的东西:
sameness = (len({x, y, z}) == 1)
This allows for any number of variables. For example:
这允许任意数量的变量。例如:
variables = {x, y, z, a, b, ...}
sameness = (len(variables) == 1)
Note:Creating a set means that each variable needs to be hashed and the hashed values need to be stored, but all()
with a generator expression is short-circuiting and keeps track of only two values at a time. Therefore, besides its readability, the generator expression is more efficient.
注意:创建一个集合意味着需要对每个变量进行散列并且需要存储散列的值,但是all()
使用生成器表达式是短路的并且一次只跟踪两个值。因此,除了可读性之外,生成器表达式更高效。
回答by Vedang Mehta
How about this?
这个怎么样?
x == y == z == 1
回答by cuonglm
Another way:
其它的办法:
sameness = all(e == 1 for e in [x, y, z])
回答by das-g
In my case, the value will either by 1 or 2, but I only need to know if they are all the same
在我的情况下,该值将是 1 或 2,但我只需要知道它们是否都相同
Is it possible to check if 1 is in each: x, y, and z? Better yet, is there a more concise way of checking simply if x, y, and z are the same value?
是否可以检查 1 是否在每个:x、y 和 z 中? 更好的是,是否有更简洁的方法来简单地检查 x、y 和 z 是否相同?
Sure:
当然:
x == y == z
which is equivalent to
这相当于
(x == y) and (y == z)
If you have an arbitrary (nonzero) number of values to compare:
如果您要比较任意(非零)数量的值:
all(values[0] == v for v in values[1:])
回答by cwhisperer
[x,y,z].count(1)
will count how many variables have 1 as value
将计算有多少变量具有 1 作为值