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

Google不允许我进行搜索,|=因此我找不到相关文档。有人知道吗

Google won’t let me search |= so I’m having trouble finding relevant documentation. Anybody know?


回答 0

|=在成对的对象之间执行就地+操作。尤其是:

在大多数情况下,它与|操作员有关。请参见下面的示例。

套装

例如,两个分配的集合的s1并并s2共享以下等效表达式:

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

其中的最终值s1等于:

  1. 分配的OR操作
  2. 就地或运算
  3. 通过特殊方法++进行就地或运算

在这里,我们将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'}

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

辞典

Python 3.9+中,在字典之间提出了新的merge(|)和update(|=)运算符。注意:这些与上述集合运算符不同。

两个指定的dict d1和之间的给定操作d2

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

其中d1经由相当于:

  1. 分配的合并权操作
  2. 就地合并权限(更新)操作;相当于d1.update(d2)

在这里,我们将合并(|)和更新(|=)应用于字典

>>> 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涉及称为数学数据结构多集(MSET)。它基本上是(对象,多重性)键值对的决定。

给定两个分配柜台之间的操作c1c2

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

其中c1经由相当于:

  1. 指定的联合操作
  2. 就地联合操作

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

在这里,我们将union(|)和就地union(|=)应用于Counters

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

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

号码

最后,您可以进行二进制数学运算。

给定的两个指定数字之间的运算n1n2

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

其中n1经由相当于:

  1. 分配的按位或运算
  2. 就地按位或运算

在这里,我们采用按位OR( |)和就地按位OR( |=),以数字

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

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

>>> # Inplace Bitwise OR, |=
>>> n1 |= n2
>>> n1
1

评论

本节简要回顾一些按位数学。在最简单的情况下,按位或运算会比较两个二进制位。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

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

>>> c = 0xa
>>> d = 0o32
>>> c | d
26

也可以看看

+ 就位按位或运算符不能应用于文字;将对象分配给名称。

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

|= performs an in-place+ operation between pairs of objects. In particular, between:

In most cases, it is related to the | operator. See examples below.

Sets

For example, the union of two assigned sets s1 and s2 share the following equivalent expressions:

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

where the final value of s1 is equivalent either by:

  1. an assigned OR operation
  2. an in-place OR operation
  3. an in-place OR operation via special method++

Example

Here we apply OR (|) and the in-place OR (|=) to sets:

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

Dictionaries

In Python 3.9+, new merge (|) and update (|=) operators are proposed between dictionaries. Note: these are not the same as set operators mentioned above.

Given operations between two assigned dicts d1 and d2:

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

where d1 is equivalent via:

  1. an assigned merge-right operation
  2. an in-place merge-right (update) operation; equivalent to d1.update(d2)

Example

Here we apply merge (|) and update (|=) to dicts:

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

Counters

The collections.Counter is related to a mathematical datastructure called a multiset (mset). It is basically a dict of (object, multiplicity) key-value pairs.

Given operations between two assigned counters c1 and c2:

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

where c1 is equivalent via:

  1. an assigned union operation
  2. an in-place union operation

A union of multisets contains the maximum multiplicities per entry. Note, this does not behave the same way as between two sets or between two regular dicts.

Example

Here we apply union (|) and the in-place union (|=) to Counters:

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

Numbers

Lastly, you can do binary math.

Given operations between two assigned numbers n1 and n2:

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

where n1 is equivalent via:

  1. an assigned bitwise OR operation
  2. an in-place bitwise OR operation

Example

Here we apply bitwise OR (|) and the in-place bitwise OR (|=) to numbers:

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

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

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

Review

This section briefly reviews some bitwise math. In the simplest case, the bitwise OR operation compares two binary bits. It will always return 1 except when both bits are 0.

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

We now extend this idea beyond binary numbers. Given any two integral numbers (lacking fractional components), we apply the bitwise OR and get an integral result:

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

How? In general, the bitwise operations follow some “rules”:

  1. internally compare binary equivalents
  2. apply the operation
  3. return the result as the given type

Let’s apply these rules to our regular integers above.

(1) Compare binary equivalents, seen here as strings (0b denotes binary):

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

(2) Apply a bitwise OR operation to each column (0 when both are 0, else 1):

01010
10000
-----
11010

(3) Return the result in the given type, e.g. base 10, decimal:

>>> int(0b11010)
26

The internal binary comparison means we can apply the latter to integers in any base, e.g. hex and octal:

>>> c = 0xa                                            # 10
>>> d = 0o20                                           # 16 
>>> c | d
26

See Also

+The in-place bitwise OR operator cannot be applied to literals; assign objects to names.

++Special methods return the same operations as their corresponding operators.


回答 1

在Python和其他许多编程语言中,|按位或运算|=是按|原样+=进行的+,即操作和分配的组合。

因此var |= value是的缩写var = var | value

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

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

In Python, and many other programming languages, | is the bitwise-OR operation. |= is to | as += is to +, i.e. a combination of operation and asignment.

So var |= value is short for var = var | value.

A common use case is to merge two sets:

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

回答 2

与集合一起使用时,它将执行并集操作。

When used with sets it performs union operation.


回答 3

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

r    s    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

This is just an OR operation between the current variable and the other one. Being T=True and F=False, see the output graphically:

r    s    r|=s
--------------
T    T    T
T    F    T
F    T    T
F    F    F

For example:

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

回答 4

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

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

It performs a binary bitwise OR of the left-hand and right-hand sides of the assignment, then stores the result in the left-hand variable.

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


回答 5

是按位还是。假设我们有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 bitwiseor with 5

It’s bitwise or. Let’s say we have 32 |= 10, picture 32 and 10 is binary.

32 = 10 0000
10 = 00 1010

Now because | is or, do a bitwise or on the two numbers

i.e 1 or 0 –> 1, 0 or 0 –> 0. Continue this down the chain

10 0000 | 00 1010 = 10 1010.

Now change the binary into a decimal, 10 1010 = 42.

For |=, think of the known examples, x +=5. It means x = x + 5, therefore if we have x |= 5, it means x = x bitwiseor with 5.


回答 6

给出用例(在花了一些时间回答其他问题之后):

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没有短路:如果您需要处理每一项并记录至少一项成功等信息,则可能很有用。

另请参阅此答案中的警告

To give a use-case (after spending time with the other answers):

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

Basically any without the short-circuiting: might be useful if you need to process every item and record at least one success etc.

See also the caveats in this answer


回答 7

在Python中,| =(ior)的作用类似于联合操作。就像x = 5和x | = 5一样,这两个值都将首先转换为二进制值,然后执行并运算,并得到答案5。

In Python,|=(ior) works like union operation. like if x=5 and x|=5 then both the value will first convert in binary value then the union operation will perform and we get the answer 5.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。