你如何获得逻辑异或 http://en.wikipedia.org/wiki/Exclusive_orPython 中的两个变量?
例如,我有两个希望是字符串的变量。我想测试其中只有一个包含 True 值(不是 None 或空字符串):
str1 = raw_input("Enter string one:")
str2 = raw_input("Enter string two:")
if logical_xor(str1, str2):
print "ok"
else:
print "bad"
The ^
运算符是按位的,并且未在所有对象上定义:
>>> 1 ^ 1
0
>>> 2 ^ 1
3
>>> "abc" ^ ""
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for ^: 'str' and 'str'
如果您已经将输入标准化为布尔值,则 != 是异或。
bool(a) != bool(b)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)