如何在Python中获得两个变量的逻辑异或?

例如,我有两个变量,我希望它们是字符串。我想测试它们中只有一个包含True值(不是None或空字符串):

str1 = raw_input("Enter string one:")
str2 = raw_input("Enter string two:")
if logical_xor(str1, str2):
    print "ok"
else:
    print "bad"

^操作符似乎是按位的,并不是在所有对象上都定义:

>>> 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'

当前回答

很多人,包括我自己,都需要一个类似于n输入异或电路的异或函数,其中n是变量。(见https://en.wikipedia.org/wiki/XOR_gate)。下面的简单函数实现了这一点。

def xor(*args):
   """
   This function accepts an arbitrary number of input arguments, returning True
   if and only if bool() evaluates to True for an odd number of the input arguments.
   """

   return bool(sum(map(bool,args)) % 2)

示例I/O如下:

In [1]: xor(False, True)
Out[1]: True

In [2]: xor(True, True)
Out[2]: False

In [3]: xor(True, True, True)
Out[3]: True

其他回答

在Python中获取两个或多个变量的逻辑异或:

将输入转换为布尔值 使用按位的异或操作符(^ or operator.xor)

例如,

bool(a) ^ bool(b)

当您将输入转换为布尔值时,按位xor变成逻辑xor。

请注意,接受的答案是错误的:!=与Python中的xor不同,因为操作符链接非常微妙。

例如,下面三个值的xor在使用!=时是错误的:

True ^  False ^  False  # True, as expected of XOR
True != False != False  # False! Equivalent to `(True != False) and (False != False)`

(附注:我尝试编辑已接受的答案,以包含这一警告,但我的更改被拒绝了。)

简单,容易理解:

sum(bool(a), bool(b)) == 1

如果一个排他性的选项是你想要的,即从n个选项中选择1个,它可以扩展为多个参数:

sum(bool(x) for x in y) == 1

Python处理逻辑操作的方式可能令人困惑,因此我的实现为用户提供了一个简单的True/False答案选项(默认情况下)。实际的Python结果可以通过将可选的第三个参数设置为None来获得。

def xor(a, b, true=True, false=False): # set true to None to get actual Python result
    ab1 = a and not b
    ab2 = not a and b
    if bool(ab1) != bool(ab2):
        return (ab1 or ab2) if true is None else true
    else:
        return false

Exclusive Or的定义如下

def xor( a, b ):
    return (a or b) and not (a and b)

按位异或已经内置在Python的operator模块中(与^ operator相同):

from operator import xor
xor(bool(a), bool(b))  # Note: converting to bools is essential