Why are "+" not clear to Python collections?

I would like to know why this is valid:

set(range(10)) - set(range(5)) 

but this is not true:

 set(range(10)) + set(range(5)) 

Is it because '+' can mean both intersection and union?

+57
python set
07 Oct 2018-11-11T00:
source share
6 answers

Python suites do not have implementations for the + operator.

You can use | for set union and & for a given intersection.

Sets are implemented - as a given difference. You can also use ^ for the symmetric difference of the sets (i.e. it will return a new set only with objects that appear in one set but do not appear in both sets).

+71
07 Oct 2018-11-11T00:
source share

Python decided to use | instead of + , because set union is a concept that is closely related to logical disjunction; Bit vectors (which are simply int / long in python) define this operation by a sequence of logical values ​​and call it "bitwise" or "bit". In fact, this operation is so similar to combining a set that binary integers are sometimes also called "bit sets", where the elements in the set are natural numbers.

Since int already defines set-like operators as | , & ^ , for a newer set type, it would be natural to use the same interface.

+72
Oct 07 '11 at 20:19
source share

Of course, they could use + to combine, but then you still need a symbol to intersect. | to combine symmetrically with & to intersect and therefore makes a better choice.

+22
Oct 07 2018-11-11T00:
source share

In set theory, the + symbol usually denotes a disconnected union of two sets. If A and B are sets, then their disconnected union is defined as the set

 A + B = {(a, 1) | a in A} U {(b, 2) | b in B} 

those. to build a disconnected union, we mark all the elements of A and all the elements of B with different tags (in the example I used numbers 1 and 2, but any two different things could do this work) and then we take the union of two result sets. In the above example, I used 'U' for set union to make it look more like regular mathematical notation; below I use Python notation, i.e. '|' for union, and '&' for intersection.

If A and B do not intersect, then A + B has a 1-to-1 correspondence with A | B. If this is not the case, then all common elements x in and B appear twice in + B: once (x, 1) and once (x, 2).

So, since the "+" symbol has a fairly established meaning as a given operation, I find it very consistent that Python does not use this symbol to combine or intersect. Python designers probably had this in mind when they selected set operators.

+20
Oct 08 2018-11-11T00:
source share

Because | means union and & means intersection. Obviously, there is no reason to add multiple statements for the same function.

Reasons to use | and & are likely returning to bitwise operations. If you represent a set as bits in a number, these are the operators you should use to combine and intersect.

+ simple is not tied to the connection, but - - set the difference.

+9
07 Oct 2018-11-11T00:
source share

Because making the difference is a very useful and well-known concept, but there is no (universally used) concept of "adding additions."

+2
Oct 07 '11 at 20:10
source share



All Articles