lettura simple

Intersection of Sets in Python

Today's lesson is all about working out how to identify the intersection within two or more sets using Python. We'll explore this through a few different approaches.

Puoi calcolare l'intersezione dei due insiemi seguendo tre strade diverse

Intersection operator A&B

Firstly, let's look at the intersection operator.

For intersecting two sets, you can turn to the & operator, as shown:

A&B

Allow me to paint a clearer picture with an example.

Assume we've got two sets, A and B, each generated using the set() function.

>>> A=set([1,2,3,4,5])
>>> B=set([4,5,6,7])

Next, to get the intersection, type C=A&B in the command line.

>>> C=A&B

This results in the intersected set A⋂B.

>>> C
{4, 5}

What the intersection does is it produces a set with elements that are found in both set A and set B, while ensuring no duplicates.

In this instance, these elements are 4 and 5. Any elements exclusive to one set do not appear in the intersection.

The intersection() method

Our second method involves the intersection() method.

Calculating the intersection between two sets is a breeze with the .intersection() method tied to the set variables.

A.intersection(B)

Let's dissect another example.

We've got two sets under the variables A and B.

>>> A=set([1,2,3,4,5])
>>> B=set([4,5,6,7])

By typing A.intersection(B) in the command line.

>>> D=A.intersection(B)

Or alternatively, using set.intersection(A,B), we can achieve the same result.

>>> D=set.intersection(A,B)

What you get is the intersected set A⋂B where both sets overlap.

>>> D
{4, 5}

The intersected set D houses all the elements present in both set A and set B, no repetitions allowed. Here, these elements are 4 and 5.

So, what's the scoop on the & operator and the intersection() method? They both serve the same purpose, providing alternative pathways to intersect two sets with identical outcomes. However, the .intersection() method has an edge, offering flexibility to apply between set variables and other iterable objects. Consider, for instance, a set variable A and a list E:

>>> A=set([1,2,3,4,5])
>>> E=[4,5,6,7]

By employing the .intersection() method on the set variable, the intersection between set A and list E is effortlessly obtained, with the result still being a set.

>>> A.intersection(E)
{4, 5}

Attempting to intersect the set variable and the list using the intersection operator A&E, Python calls out an error because the & operator is strictly for two set variables.

>>> A&E
Traceback (most recent call last):
TypeError: unsupported operand type(s) for &: 'set' and 'list'

In addition, the intersection() method is capable of intersecting several sets simultaneously. For example, we define three sets A, B, and C

>>> A=set([1,2,3,4,5])
>>> B=set([4,5,6,7])
>>> C=set([2,4,6,8])

Afterward, we intersect these three sets using the intersection() method

>>> D=A.intersection(B,C)

The result is the intersected set D=A⋂B⋂C

In this case, the only element present across all sets is number 4

>>> D
{4}

The intersection_update() method

The intersection_update() method intersects two or more sets, altering the set it's applied to.

A.intersection_update(B)

Let me illustrate this with an example.

We create three sets under the variables A, B, and C

>>> A=set([1,2,3,4,5])
>>> B=set([4,5,6,7])
>>> C=set([2,4,6,8])

Then, to get the intersection among the three sets, type A.intersection(B,C).

>>> A.intersection_update(B,C)

This leads us to the intersected set A=A⋂B⋂C

The resulting set contains the single common element, which is number 4

>>> A
{4}

Here, the intersection modifies the original content of set A.

So, how do the intersection and intersection_update methods compare? While the two are similar, the intersection_update method stands apart in that it modifies the set to which it's applied, unlike the intersection() method which intersects sets without altering them, always returning a new set as a result.

With this information, you should be well-prepared to handle set intersections in Python.




Report a mistake or post a question




FacebookTwitterLinkedinLinkedin