[英]Given 2 int values, return True if one is negative and other is positive
def logical_xor(a, b): # for example, -1 and 1
print (a < 0) # evaluates to True
print (b < 0) # evaluates to False
print (a < 0 != b < 0) # EVALUATES TO FALSE! why??? it's True != False
return (a < 0 != b < 0) # returns False when it should return True
print ( logical_xor(-1, 1) ) # returns FALSE!
# now for clarification
print ( True != False) # PRINTS TRUE!
Could someone explain what is happening?有人可以解释发生了什么吗? I'm trying to make a one liner:
我正在尝试制作一个班轮:
lambda a, b: (a < 0 != b < 0)
All comparison operators in Python have the same precedence. Python 中的所有比较运算符都具有相同的优先级。 In addition, Python does chained comparisons.
此外,Python 会进行链式比较。 Thus,
因此,
(a < 0 != b < 0)
breaks down as:分解为:
(a < 0) and (0 != b) and (b < 0)
If any one of these is false, the total result of the expression will be False
.如果其中任何一项为假,则表达式的总结果将为
False
。
What you want to do is evaluate each condition separately, like so:您要做的是分别评估每个条件,如下所示:
(a < 0) != (b < 0)
Other variants, from comments:其他变体,来自评论:
(a < 0) is not (b < 0) # True and False are singletons so identity-comparison works
(a < 0) ^ (b < 0) # bitwise-xor does too, as long as both sides are boolean
(a ^ b < 0) # or you could directly bitwise-xor the integers;
# the sign bit will only be set if your condition holds
# this one fails when you mix ints and floats though
(a * b < 0) # perhaps most straightforward, just multiply them and check the sign
Your code doesn't work as intended because !=
takes higher precedence than a < 0
and b < 0
.您的代码无法按预期工作,因为 !=
优先级高于a < 0
和b < 0
。As itzmeontv suggests in his answer, you can simply decide the precedence yourself by surrounding logical components with parentheses:正如 itzmeontv 在他的回答中所建议的那样,您可以通过用括号将逻辑组件括起来来简单地自己决定优先级:
(a < 0) != (b < 0)
Your code attempts to evaluate a < (0 != b) < 0
您的代码尝试评估a < (0 != b) < 0
[EDIT] [编辑]
As tzaman rightly points out, the operators have the same precedence, but your code is attempting to evaluate (a < 0) and (0 != b) and (b < 0)
.正如 tzaman 正确指出的那样,运算符具有相同的优先级,但您的代码正在尝试评估
(a < 0) and (0 != b) and (b < 0)
。 Surrounding your logical components with parentheses will resolve this:用括号包围你的逻辑组件将解决这个问题:
(a < 0) != (b < 0)
Operator precedence: https://docs.python.org/3/reference/expressions.html#operator-precedence运算符优先级: https : //docs.python.org/3/reference/expressions.html#operator-precedence
Comparisons (ia chaining): https://docs.python.org/3/reference/expressions.html#not-in比较(ia 链接): https : //docs.python.org/3/reference/expressions.html#not-in
You can use this你可以用这个
return (a < 0) != (b < 0)
Comparisons can be chained arbitrarily, eg, 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是假的)。
So it becomes所以它变成
(a < 0) and (0 != b) and (b < 0)
See https://docs.python.org/3/reference/expressions.html#not-in见https://docs.python.org/3/reference/expressions.html#not-in
In Python, comparison operators are of the same precedence, and they are non-associative.在 Python 中,比较运算符具有相同的优先级,并且它们是非关联的。 There is a separate rule for sequences of comparison operators, the chaining rule.
比较运算符的序列有一个单独的规则,即链接规则。 Python documentation states about that:
Python 文档说明了这一点:
if
a, b, c, ..., y, z
are expressions andop1, op2, ..., opN
are comparison operators, then aop1 b op2 c ... y opN z
is equivalent toa op1 b and b op2 c and ... y opN z
, except that each expression is evaluated at most once.如果
a, b, c, ..., y, z
是表达式并且op1, op2, ..., opN
是比较运算符,那么 aop1 b op2 c ... y opN z
等价于a op1 b and b op2 c and ... y opN z
,除了每个表达式最多计算一次。
Further, a op1 b and b op2 c and ... y opN z
evaluates left to right.此外,
a op1 b and b op2 c and ... y opN z
从左到右求值。
a < 0 and 0 != b and b < 0
a < 0
will evaluated to False
, and the further evaluation will be stopped due to short-circuit evaluation . a < 0
将评估为False
,并且由于短路评估将停止进一步评估。 So, the whole expression will be evaluated as False
.因此,整个表达式将被评估为
False
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.