谷歌不让我搜索|=,所以我很难找到相关的文件。有人知道吗?


当前回答

给出一个用例(在花时间分析其他答案之后):

def process(item):
   return bool(item) # imagine some sort of complex processing taking place above

def any_success(data): # return True if at least one is successful
    at_least_one = False
    for item in data:
       at_least_one |= process(item)
    return at_least_one

>>> any_success([False, False, False])
False
>>> any_success([True, False, False])
True
>>> any_success([False, True, False])
True

基本上没有短路:如果你需要处理每个项目并记录至少一次成功,可能会有用。

请参见此回答中的注意事项

其他回答

它是位或。 假设我们有32 | 10,图片32和10的二进制:

32 = 10 0000
10 = 00 1010

现在,因为|是“或”运算,对这两个数字进行“或”运算

即1或0——> 1 0或0——> 0。沿着这个链条继续下去:

10 0000 | 00 1010 = 10 1010.

现在把二进制变成十进制,10 1010 = 42。

对于|=,考虑已知的例子,x +=5。这意味着x = x + 5,因此如果我们有x |= 5,这意味着x = x位或5。

给出一个用例(在花时间分析其他答案之后):

def process(item):
   return bool(item) # imagine some sort of complex processing taking place above

def any_success(data): # return True if at least one is successful
    at_least_one = False
    for item in data:
       at_least_one |= process(item)
    return at_least_one

>>> any_success([False, False, False])
False
>>> any_success([True, False, False])
True
>>> any_success([False, True, False])
True

基本上没有短路:如果你需要处理每个项目并记录至少一次成功,可能会有用。

请参见此回答中的注意事项

在Python和许多其他编程语言中,|是位或操作。|=之于|,正如+=之于+,即运算和赋值的组合。

var |= value是var = var | value的缩写。

一个常见的用例是合并两个集合:

>>> a = {1,2}; a |= {3,4}; print(a)
{1, 2, 3, 4}

它表示按位或操作。

例子:

x = 5
x |= 3 #which is similar to x = x | 3
print(x)

答案:7

它是如何工作的?

The binary of 5 is : 0 1 0 1
The binary of 3 is : 0 0 1 1

OR operation : (If one of both sides are 1/True then result is 1/True)

0 1 0 1  #Binary of 5
0 0 1 1  #Binary of 3
---------------------
0 1 1 1  #Binary of 7

所以答案是7

希望这也能帮助其他人理解:

dict1 = {'a': 'dict1', 'b': 'dict1', 'c': 'dict1'}
dict2 = {'c': 'dict2', 'd': 'dict2', 'e': 'dict2'}

dict3 = dict1.copy()
dict3 = dict3 | dict2
dict4 = dict1.copy()
dict4 |= dict2
print(f'dict1:\n {dict1}')
print(f'dict2:\n {dict2}')
print(f'dict1 after dict1 = dict1 | dict2 (dict2 index c replaces dict1 index c, items in dict1 are discarded if present in dict2):\n {dict3}')
print(f'dict1 after dict1 |= dict2 (same behaviour as dict1 = dict1 | dict2):\n {dict4}')

dict5 = dict1.copy()
dict5 = dict2 | dict5
dict6 = dict2.copy()
dict6 |= dict1
print(f'dict1 after dict1 = dict2 | dict1 (dict2 index c is missing, dict1 index c was retained, items in dict2 are discarded if present in dict1):\n {dict5}')
print(f'dict2 after dict2 |= dict1 (same behaviour as dict2 = dict2 | dict1):\n {dict6}')


dict1:
 {'a': 'dict1', 'b': 'dict1', 'c': 'dict1'}
dict2:
 {'c': 'dict2', 'd': 'dict2', 'e': 'dict2'}
dict1 after dict1 = dict1 | dict2 (dict2 index c replaces dict1 index c, items in dict1 are discarded if present in dict2):
 {'a': 'dict1', 'b': 'dict1', 'c': 'dict2', 'd': 'dict2', 'e': 'dict2'}
dict1 after dict1 |= dict2 (same behaviour as dict1 = dict1 | dict2):
 {'a': 'dict1', 'b': 'dict1', 'c': 'dict2', 'd': 'dict2', 'e': 'dict2'}
dict1 after dict1 = dict2 | dict1 (dict2 index c is missing, dict1 index c was retained, items in dict2 are discarded if present in dict1):
 {'c': 'dict1', 'd': 'dict2', 'e': 'dict2', 'a': 'dict1', 'b': 'dict1'}
dict2 after dict2 |= dict1 (same behaviour as dict2 = dict2 | dict1):
 {'c': 'dict1', 'd': 'dict2', 'e': 'dict2', 'a': 'dict1', 'b': 'dict1'}