Python 测试三个值的相等性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13805939/
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
Testing equality of three values
提问by graywolf
Does this do what I think it does? It seems to me that yes. I am asking to be sure.
这是否符合我的想法?在我看来是的。我要求确定。
if n[i] == n[i+1] == n[i+2]:
return True
Are these equal?
这些是平等的吗?
if n[i] == n[i+1] and n[i+1] == n[i+2]:
return True
采纳答案by Ignacio Vazquez-Abrams
It is equivalentto but not equal to, since accesses are only performed once. Python chains relational operators naturally (including inand is).
它相当于但不等于,因为访问只执行一次。Python 自然地链接关系运算符(包括in和is)。
The easiest way to show the slight difference:
显示细微差别的最简单方法:
>>> print(1) == print(2) == print(3)
1
2
3
True
>>> print(1) == print(2) and print(2) == print(3)
1
2
2
3
True
print()always returns None, so all we are doing is comparing Nones here, so the result is always True, but note that in the second case, print(2)is called twice, so we get two 2s in the output, while in the first case, the result is used for both comparisons, so it is only executed once.
print()总是返回None,所以我们所做的就是None在这里比较s,所以结果总是True,但请注意,在第二种情况下,print(2)被调用了两次,所以我们2在输出中得到两个s,而在第一种情况下,使用结果对于两个比较,所以它只执行一次。
If you use pure functions with no side-effects, the two operations end up exactly the same, but otherwise they are a little different.
如果您使用没有副作用的纯函数,这两个操作最终会完全相同,但除此之外它们有点不同。
回答by Joran Beasley
yes you are correct ....
是的,你是对的 ....
you can also do
你也可以这样做
5 > x > 1
or
或者
1 < x < 5
回答by iabdalkader
Yes, however, when the comparisons are chained the common expressionis evaluated once, when using andit's evaluated twice. In both cases the second comparison is not evaluated if the first one is false, example from the docs:
是的,但是,当比较链接时,公共表达式会计算一次,使用时and会计算两次。在这两种情况下,如果第一个比较为假,则不会评估第二个比较,例如来自文档:
Comparisons can be chained arbitrarily, e.g., x < y <= z is equivalent to x < y and y <= z, except that y is evaluated only once (but in both cases z is not evaluated at all when x < y is found to be false).
比较可以任意链接,例如,x < y <= z 等价于 x < y 和 y <= z,除了 y 只计算一次(但在这两种情况下,当找到 x < y 时根本不计算 z是假的)。
回答by VB9-UANIC
Yep, at the python's internals the comparison operators returns nor true neither false, they instead return the 'comparison result' object (cannot remember the class name, it was quite in past), and this object provides the _lt_, _gt_, _eq_etc etc methods and become 'responsible' for the final result (and the 'comparison result' is casting to True or False at end of statement). That's a magic of semantic control python provides to you :)
是的,在 python 的内部,比较运算符返回也不是真或假,而是返回“比较结果”对象(不记得类名,它已经过去了),并且该对象提供了_ lt_,_ gt_, _ eq_etc 等方法并成为最终结果的“负责人”(并且“比较结果”在语句结束时转换为 True 或 False)。这是python为您提供的语义控制的魔力:)
回答by Aaron Dunigan AtLee
As answered by others, the answer is yes. However: beware of adding parentheses. For example:
正如其他人所回答的,答案是肯定的。但是:注意添加括号。例如:
>>> 1 == 2 == 0
False
>>> (1 == 2) == 0
True
In the second case, (1 == 2)evaluates to False, and then False == 0evaluates to True, because Python allows comparison of booleans to integers.
在第二种情况下,(1 == 2)计算为False,然后False == 0计算为True,因为 Python 允许将布尔值与整数进行比较。
Likewise:
同样地:
>>> 0 == 0 == 1
False
>>> (0 == 0) == 1
True
回答by Pikesh Prasoon
You can use set collection to test the equality
您可以使用集合来测试相等性
>>> a, b, c = 2, 2, 2
>>> len({a, b, c}) == 1
True
>>> a, b, c = 2, 2, 3
>>> len({a, b, c}) == 1
False
回答by Q. Qiao
It seems that you can also chain == with !=:
似乎你也可以用 != 链接 ==:
>>> a = b = c = 1
>>> a == b == c
True
>>> a == b == c == 1
True
>>> a == b == c == 2
False
>>> a == b == c != 2
True
>>> a == b == c != 1
False
>>>

