Zac*_*sch 597 python logical-operators
你如何在Python中获得两个变量的逻辑xor?
例如,我有两个我期望成为字符串的变量.我想测试只有其中一个包含True值(不是None或空字符串):
str1 = raw_input("Enter string one:")
str2 = raw_input("Enter string two:")
if logical_xor(str1, str2):
print "ok"
else:
print "bad"
Run Code Online (Sandbox Code Playgroud)
该^
运营商似乎是按位,并在所有对象没有定义:
>>> 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'
Run Code Online (Sandbox Code Playgroud)
A. *_*ady 1113
如果你已经将输入规范化为布尔值,那么!=是xor.
bool(a) != bool(b)
Run Code Online (Sandbox Code Playgroud)
Zac*_*sch 448
您始终可以使用xor的定义从其他逻辑操作计算它:
(a and not b) or (not a and b)
Run Code Online (Sandbox Code Playgroud)
但这对我来说有点过于冗长,乍一看并不是特别清楚.另一种方法是:
bool(a) ^ bool(b)
Run Code Online (Sandbox Code Playgroud)
两个布尔值上的xor运算符是逻辑xor(与int不同,它是按位的).这是有道理的,因为bool
它只是一个子类int
,但实现只有值0
和1
.当域被限制为0
和时,逻辑xor等效于按位xor 1
.
所以logical_xor
函数将实现如下:
def logical_xor(str1, str2):
return bool(str1) ^ bool(str2)
Run Code Online (Sandbox Code Playgroud)
sin*_*boy 173
在operator
模块中独占或已经内置到Python :
from operator import xor
xor(bool(a), bool(b)) # Note: converting to bools is essential
Run Code Online (Sandbox Code Playgroud)
dda*_*daa 39
正如扎克解释的那样,你可以使用:
xor = bool(a) ^ bool(b)
Run Code Online (Sandbox Code Playgroud)
就个人而言,我赞成略有不同的方言:
xor = bool(a) + bool(b) == 1
Run Code Online (Sandbox Code Playgroud)
这种方言的灵感来自于我在学校学到的逻辑图表语言,其中"OR"由包含?1
(大于或等于1)的框表示,"XOR"由包含的框表示=1
.
这具有正确实现独占或多个操作数的优点.
nos*_*klo 24
or
:A or B
:回报A
,如果bool(A)
是True
,否则返回B
and
:A and B
:回报A
,如果bool(A)
是False
,否则返回B
为了保持大部分的思维方式,我的逻辑xor定义是:
def logical_xor(a, b):
if bool(a) == bool(b):
return False
else:
return a or b
Run Code Online (Sandbox Code Playgroud)
这样,它可以返回a
,b
或False
:
>>> logical_xor('this', 'that')
False
>>> logical_xor('', '')
False
>>> logical_xor('this', '')
'this'
>>> logical_xor('', 'that')
'that'
Run Code Online (Sandbox Code Playgroud)
Rug*_*nar 21
我已经测试了几种方法并且not a != (not b)
似乎是最快的.
这是一些测试
%timeit not a != (not b)
10000000 loops, best of 3: 78.5 ns per loop
%timeit bool(a) != bool(b)
1000000 loops, best of 3: 343 ns per loop
%timeit not a ^ (not b)
10000000 loops, best of 3: 131 ns per loop
Run Code Online (Sandbox Code Playgroud)
您使用与 C 中相同的 XOR 运算符,即^
.
我不知道为什么,但最受支持的解决方案建议bool(A) != bool(B)
,而我会说 - 与 C 的运算符一致^
,最明显的解决方案是:
bool(A) ^ bool(B)
Run Code Online (Sandbox Code Playgroud)
对于来自任何语言C
或任何C
派生语言的任何人来说,这都更具可读性并且可以立即理解......
在进行代码高尔夫时,可能
not A ^ (not B)
Run Code Online (Sandbox Code Playgroud)
将成为赢家。使用not
as 布尔值转换器(小于 1 个字母bool()
。在某些情况下,对于第一个表达式,可以省略括号。好吧,这取决于,在必须这样做的情况下not(A) ^ (not(B))
,bool()
需要相同数量的字母......
小智 8
奖励线程:
Anoder想法...只是你尝试(可能)pythonic表达«不是»以获得逻辑«xor»的行为
真相表将是:
>>> True is not True
False
>>> True is not False
True
>>> False is not True
True
>>> False is not False
False
>>>
Run Code Online (Sandbox Code Playgroud)
对于您的示例字符串:
>>> "abc" is not ""
True
>>> 'abc' is not 'abc'
False
>>> 'abc' is not ''
True
>>> '' is not 'abc'
True
>>> '' is not ''
False
>>>
Run Code Online (Sandbox Code Playgroud)
然而; 如上所述,它取决于你想要拉出任何几个字符串的实际行为,因为字符串不是boleans ......甚至更多:如果你"潜入Python",你会发现«The Peculiar nature of"和"和"或"» http://www.diveintopython.net/power_of_introspection/and_or.html
对不起,我写了英文,这不是我天生的语言.
问候.
要在 Python 中获取两个或多个变量的逻辑异或:
^
或运算符 ( or operator.xor
)例如,
bool(a) ^ bool(b)
Run Code Online (Sandbox Code Playgroud)
当您将输入转换为布尔值时,按位异或变为逻辑异或。
请注意,接受的答案是错误的: !=
由于operator chaining的微妙之处,因此与 Python 中的 xor 不同。
例如,以下三个值的异或在使用时是错误的!=
:
True ^ False ^ False # True, as expected of XOR
True != False != False # False! Equivalent to `(True != False) and (False != False)`
Run Code Online (Sandbox Code Playgroud)
(PS 我尝试编辑接受的答案以包含此警告,但我的更改被拒绝。)
独家或定义如下
def xor( a, b ):
return (a or b) and not (a and b)
Run Code Online (Sandbox Code Playgroud)
小智 7
因为我没有看到xor的简单变体使用变量参数并且只对Truth值操作True或False,所以我只是把它扔到这里供任何人使用.正如其他人所指出的那样,非常(不是说非常)直截了当.
def xor(*vars):
sum = False
for v in vars:
sum = sum ^ bool(v)
return sum
Run Code Online (Sandbox Code Playgroud)
用法也很简单:
if xor(False, False, True, False):
print "Hello World!"
Run Code Online (Sandbox Code Playgroud)
因为这是广义的n元逻辑XOR,所以只要True操作数的数量是奇数,它的真值就是True(并且不仅当正好一个是True时,这只是n-ary XOR为True的一种情况).
因此,如果您正在搜索只有其中一个操作数的只有True的n-ary谓词,您可能希望使用:
def isOne(*vars):
sum = False
for v in vars:
if sum and v:
return False
else:
sum = sum or v
return sum
Run Code Online (Sandbox Code Playgroud)
有时我发现自己使用1和0而不是布尔值True和False值.在这种情况下,xor可以定义为
z = (x + y) % 2
Run Code Online (Sandbox Code Playgroud)
其中有以下真值表:
x
|0|1|
-+-+-+
0|0|1|
y -+-+-+
1|1|0|
-+-+-+
Run Code Online (Sandbox Code Playgroud)
小智 7
我知道这已经很晚了,但我有一个想法,它可能是值得的,只是为了文档.也许这会奏效:np.abs(x-y)
这个想法就是这样
简单易懂:
sum( (bool(a), bool(b) ) == 1
Run Code Online (Sandbox Code Playgroud)
如果你所追求的是独家选择,它可以扩展为多个参数:
sum( bool(x) for x in y ) % 2 == 1
Run Code Online (Sandbox Code Playgroud)
这个怎么样?
(not b and a) or (not a and b)
Run Code Online (Sandbox Code Playgroud)
将给a
如果b
是假
会给b
如果a
是假
会给False
否则
或者使用Python 2.5+三元表达式:
(False if a else b) if b else a
Run Code Online (Sandbox Code Playgroud)
小智 6
这里建议的一些实现将导致在某些情况下重复评估操作数,这可能导致意外的副作用,因此必须避免.
这就是说,一个xor
返回要么执行True
或False
相当简单; 如果可能的话,返回其中一个操作数的方法要复杂得多,因为对于哪个操作数应该是所选操作数没有共识,特别是当有两个以上的操作数时.例如,应该xor(None, -1, [], True)
返回None
,[]
还是False
?我敢打赌,每个答案对某些人来说都是最直观的答案.
对于True或False结果,有多达五种可能的选择:返回第一个操作数(如果它匹配值的最终结果,否则为boolean),返回第一个匹配(如果至少存在一个,则为boolean),返回最后一个操作数(if ... else ...),返回最后一个匹配(if ... else ...),或者总是返回boolean.总而言之,那是5**2 = 25种口味xor
.
def xor(*operands, falsechoice = -2, truechoice = -2):
"""A single-evaluation, multi-operand, full-choice xor implementation
falsechoice, truechoice: 0 = always bool, +/-1 = first/last operand, +/-2 = first/last match"""
if not operands:
raise TypeError('at least one operand expected')
choices = [falsechoice, truechoice]
matches = {}
result = False
first = True
value = choice = None
# avoid using index or slice since operands may be an infinite iterator
for operand in operands:
# evaluate each operand once only so as to avoid unintended side effects
value = bool(operand)
# the actual xor operation
result ^= value
# choice for the current operand, which may or may not match end result
choice = choices[value]
# if choice is last match;
# or last operand and the current operand, in case it is last, matches result;
# or first operand and the current operand is indeed first;
# or first match and there hasn't been a match so far
if choice < -1 or (choice == -1 and value == result) or (choice == 1 and first) or (choice > 1 and value not in matches):
# store the current operand
matches[value] = operand
# next operand will no longer be first
first = False
# if choice for result is last operand, but they mismatch
if (choices[result] == -1) and (result != value):
return result
else:
# return the stored matching operand, if existing, else result as bool
return matches.get(result, result)
testcases = [
(-1, None, True, {None: None}, [], 'a'),
(None, -1, {None: None}, 'a', []),
(None, -1, True, {None: None}, 'a', []),
(-1, None, {None: None}, [], 'a')]
choices = {-2: 'last match', -1: 'last operand', 0: 'always bool', 1: 'first operand', 2: 'first match'}
for c in testcases:
print(c)
for f in sorted(choices.keys()):
for t in sorted(choices.keys()):
x = xor(*c, falsechoice = f, truechoice = t)
print('f: %d (%s)\tt: %d (%s)\tx: %s' % (f, choices[f], t, choices[t], x))
print()
Run Code Online (Sandbox Code Playgroud)
Python具有按位异或运算符,它是^
:
>>> True ^ False
True
>>> True ^ True
False
>>> False ^ True
True
>>> False ^ False
False
Run Code Online (Sandbox Code Playgroud)
您可以通过在应用xor(^
)之前将输入转换为布尔值来使用它:
bool(a) ^ bool(b)
Run Code Online (Sandbox Code Playgroud)
(编辑-感谢Arel)
这就是我编写任何真值表的方法。特别是对于异或,我们有:
| a | b | xor | |
|---|----|-------|-------------|
| T | T | F | |
| T | F | T | a and not b |
| F | T | T | not a and b |
| F | F | F | |
Run Code Online (Sandbox Code Playgroud)
只需查看答案栏中的 T 值,然后用逻辑“或”将所有正确的情况串起来。因此,这个真值表可以在情况 2 或 3 中产生。因此,
xor = lambda a, b: (a and not b) or (not a and b)
Run Code Online (Sandbox Code Playgroud)
Xor^
在 Python 中。它返回:
__xor__
。如果您打算在字符串上使用它们,则将它们转换为bool
使您的操作明确无误(您也可以表示set(str1) ^ set(str2)
)。
Many folks, including myself, need an xor
function that behaves like an n-input xor circuit, where n is variable. (See https://en.wikipedia.org/wiki/XOR_gate). The following simple function implements this.
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)
Run Code Online (Sandbox Code Playgroud)
Sample I/O follows:
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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
649394 次 |
最近记录: |