Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python编写布尔表达式_Python_Boolean Logic_Boolean Expression - Fatal编程技术网

使用Python编写布尔表达式

使用Python编写布尔表达式,python,boolean-logic,boolean-expression,Python,Boolean Logic,Boolean Expression,我试图用Python编写一个布尔表达式,但Python似乎只能用位运算来实现XOR表达式 在没有XOR运算符的情况下,用Python编写此表达式的最佳方法是什么 (A ^ B ^ C ^ D) U ((B U C U D)' XOR A) 编辑: 我尝试过: if (A and B and C and D) or ((A and not (B or C or D)) or (not A and (B and C and D))): 我希望简化它。只需使用按位^运算符。当^-ed在一起时,Py

我试图用Python编写一个布尔表达式,但Python似乎只能用位运算来实现XOR表达式

在没有XOR运算符的情况下,用Python编写此表达式的最佳方法是什么

(A ^ B ^ C ^ D) U ((B U C U D)' XOR A)
编辑:

我尝试过:

if (A and B and C and D) or ((A and not (B or C or D)) or (not A and (B and C and D))):

我希望简化它。

只需使用按位
^
运算符。当
^
-ed在一起时,Python的布尔值返回布尔值:

>>> True ^ True
False
>>> True ^ False
True
运算符主要用于支持短路,但XOR不能短路

还有
=

>>> True != True
False
>>> True != False
True
但当链接到更多参数时,这并不能满足您的需要:

>>> True != True != True
False

只需使用按位
^
运算符。当
^
-ed在一起时,Python的布尔值返回布尔值:

>>> True ^ True
False
>>> True ^ False
True
运算符主要用于支持短路,但XOR不能短路

还有
=

>>> True != True
False
>>> True != False
True
但当链接到更多参数时,这并不能满足您的需要:

>>> True != True != True
False
(A和B以及C和D)或((A和非(B或C或D))或(非A和(B和C和D))
将简化为:
(B和C和D)或(A和非(B或C或D))
(A和B和C和D)或((A和非(B或C或D))或(非A和(B和C和D))
将简化为:
(B和C和D)或(A和非(B或C或D))

您尝试了什么?请查看。上面有一个很好的页面。经过一些修改,你几乎可以把它写出来。如果您不想使用二进制表达式,可以使用集合库。您尝试了什么?请查看。上面有一个很好的页面。经过一些修改,你几乎可以把它写出来。如果不想使用二进制表达式,可以使用集合库。