|= (ior)在Python中做什么?

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

146159 次浏览

在Python和许多其他编程语言中,|bitwise-OR操作|=之于|,就像+=之于+一样,即操作和赋值的组合。

所以var |= valuevar = var | value的缩写。

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

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

它对赋值的左侧和右侧执行二进制位或运算,然后将结果存储在左侧变量中。

http://docs.python.org/reference/expressions.html#binary-bitwise-operations

这只是当前变量和另一个变量之间的OR运算。作为T=TrueF=False,以图形方式查看输出:

r 年代 r|=s
T T T
T F T
F T T
F F F

例如:

>>> r=True
>>> r|=False
>>> r
True
>>> r=False
>>> r|=False
>>> r
False
>>> r|=True
>>> r
True

当与set一起使用时,它执行联合操作。

|=在对象对之间执行in-place+操作。特别是,在:

在大多数情况下,它与|操作符相关。请看下面的例子。

例如,两个赋值集s1s2的并集共享以下等价表达式:

>>> s1 = s1 | s2                                           # 1
>>> s1 |= s2                                               # 2
>>> s1.__ior__(s2)                                         # 3

其中s1的最终值可以通过以下方式等价:

  1. 指定的或运算
  2. 就地或就地操作
  3. 通过特殊方法__abc0的就地或操作

例子

在这里,我们将OR (|)和原位OR (|=)应用于:

>>> s1 = {"a", "b", "c"}
>>> s2 = {"d", "e", "f"}


>>> # OR, |
>>> s1 | s2
{'a', 'b', 'c', 'd', 'e', 'f'}
>>> s1                                                     # `s1` is unchanged
{'a', 'b', 'c'}


>>> # In-place OR, |=
>>> s1 |= s2
>>> s1                                                     # `s1` is reassigned
{'a', 'b', 'c', 'd', 'e', 'f'}

字典

Python 3.9 +中,字典之间提出了新的合并(|)和更新(|=)操作符。注意:这些操作符与上面提到的集合操作符不同。

给定两个赋值字典d1d2之间的操作:

>>> d1 = d1 | d2                                           # 1
>>> d1 |= d2                                               # 2

其中d1等价于via:

  1. 指定的合并权限操作
  2. 就地合并-权利(更新)操作;等价于d1.update(d2)

例子

这里我们对字典应用merge (|)和update (|=):

>>> d1 = {"a": 0, "b": 1, "c": 2}
>>> d2 = {"c": 20, "d": 30}


>>> # Merge, |
>>> d1 | d2
{"a": 0, "b": 1, "c": 20, "d": 30}
>>> d1
{"a": 0, "b": 1, "c": 2}


>>> # Update, |=
>>> d1 |= d2
>>> d1
{"a": 0, "b": 1, "c": 20, "d": 30}

计数器

collections.Counter与一个名为< em >多重集< / em > (mset)的数学数据结构相关。它基本上是(对象,多重性)键-值对字典。

给定两个指定计数器c1c2之间的操作:

>>> c1 = c1 | c2                                           # 1
>>> c1 |= c2                                               # 2

其中c1等价于via:

  1. 指定的联合操作
  2. 现场工会操作

多集并包含每个条目的最大多样性。注意,这与两个集合之间或两个常规字典之间的行为不同。

例子

这里我们将union (|)和in-place union (|=)应用于计数器:

import collections as ct




>>> c1 = ct.Counter({2: 2, 3: 3})
>>> c2 = ct.Counter({1: 1, 3: 5})


>>> # Union, |
>>> c1 | c2
Counter({2: 2, 3: 5, 1: 1})
>>> c1
Counter({2: 2, 3: 3})


>>> # In-place Union, |=
>>> c1 |= c2
>>> c1
Counter({2: 2, 3: 5, 1: 1})

数字

最后,你可以做二进制数学。

给定两个已分配数字n1n2之间的运算:

>>> n1 = n1 | n2                                           # 1
>>> n1 |= n2                                               # 2

其中n1等价于via:

  1. 指定的位或操作
  2. 就地按位或操作

例子

这里我们对数字应用按位OR (|)和按位OR (|=):

>>> n1 = 0
>>> n2 = 1


>>> # Bitwise OR, |
>>> n1 | n2
1
>>> n1
0


>>> # In-place Bitwise OR, |=
>>> n1 |= n2
>>> n1
1

审查

本节简要回顾一些位数学。在最简单的情况下,按位OR操作比较两个二进制位。它总是返回1,除非两个位都是0

>>> assert 1 == (1 | 1) == (1 | 0) == (0 | 1)
>>> assert 0 == (0 | 0)

现在我们将这个概念扩展到二进制数之外。给定任意两个整数(没有分数分量),我们应用位或运算,得到一个积分结果:

>>> a = 10
>>> b = 16
>>> a | b
26

如何?一般来说,位操作遵循一些“规则”:

  1. 内部比较二进制等价物
  2. 应用操作
  3. 返回作为给定类型的结果

让我们把这些规则应用到上面的正则整数上。

(1)比较二进制等价物,在这里被视为字符串(0b表示二进制):

>>> bin(a)
'0b1010'
>>> bin(b)
'0b10000'

(2)对每一列应用按位或操作(当两列都为0时为0,否则为1):

01010
10000
-----
11010

(3)返回给定类型的结果,例如以10为基数,十进制:

>>> int(0b11010)
26

内部二进制比较意味着我们可以将后者应用于任何进制的整数,例如十六进制和八进制:

>>> a = 10                                   # 10, dec
>>> b = 0b10000                              # 16, bin
>>> c = 0xa                                  # 10, hex
>>> d = 0o20                                 # 16, oct


>>> a | b
26
>>> c | d
26

另请参阅

+就地按位OR操作符不能应用于字面量;为对象分配名称。

++特殊方法返回与其对应的操作符相同的操作。

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

32 = 10 0000
10 = 00 1010

现在因为|是OR操作,所以对这两个数字进行位或运算

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

10 0000 | 00 1010 = 10 1010.

现在将二进制转换为十进制,10 1010 = 42

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

在Python中,|=(ior)类似于联合运算。 就像如果x=5和x|=5,那么两个值都将首先转换为二进制值,然后执行并集操作,我们得到答案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

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

另见这个答案中的警告

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

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

表示按位或操作

例子:

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