Python language basics 52: basic set algebra operations

Introduction

In the previous post we saw how to remove an object from a set in Python. We discussed the usage of two methods, namely ‘remove’ and ‘discard’ and how they differed.

In this post we’ll go through 4 useful functions from the world of set algebra.

Set algebra

You must have come across sets in your basic algebra studies at school. Remember how your teacher demonstrated things like ‘unions’ or ‘intersections’ using circles or ellipses? Here’s an image from Wikipedia showing a set intersection:

Intersection of sets example

Set algebra is a large topic. You can read more about it on Wikipedia here and here. In this post we’ll go through 4 basic operations.

Intersection

An intersection ” is the set that contains all elements of A that also belong to B (or equivalently, all elements of B that also belong to A), but no other elements.”

That’s quite simple I guess. Python has the ‘intersection’ method that returns the intersection of two sets:

set_a = {4, 6, 3, 5, 9, 8, 7}
set_b = {6, 5, 7, 1, 2}
set_intersection = set_a.intersection(set_b)

The resulting set will have the following elements:

{5, 6, 7}

…which are the ones that figure in both sets.

Union

A set union is the collection of all unique elements of two sets. If an element figures in both sets it will appear only once in the union set:

set_a = {4, 6, 3, 5, 9, 8, 7}
set_b = {6, 5, 7, 1, 2}
set_union = set_a.union(set_b)

‘set_union’ will hold the following integers:

{1, 2, 3, 4, 5, 6, 7, 8, 9}

…which are the ones that are unique across the constituent sets, i.e. set_a and set_b.

Complement

A complement is a set of elements that only figure in one of the sets but not both. Complement sets can be calculated using the ‘difference’ function in Python:

set_a = {4, 6, 3, 5, 9, 8, 7}
set_b = {6, 5, 7, 1, 2}
set_a_difference = set_a.difference(set_b)
set_b_difference = set_b.difference(set_a)

‘set_a_difference’ will contain the following objects:

{8, 9, 3, 4}

…which are the ones that exclusively appear in set_a but not set_b. Conversely, ‘set_b_difference’ will hold the values that are only found in set_b but not set_a, that is…

{1, 2}

Symmetric difference

A symmetric difference is the exact opposite of an intersection: “the set of elements which are in either of the sets and not in their intersection.”

Let’s see what the symmetric difference of the two integer sets gives us:

set_a = {4, 6, 3, 5, 9, 8, 7}
set_b = {6, 5, 7, 1, 2}
set_symm_difference = set_a.symmetric_difference(set_b)

The resulting set contains the following:

{1, 2, 3, 4, 8, 9}

1 and 2 figure in set_b but not set_a. 3, 4, 8, 9 on the other hand appear in set_a but not set_b. The other elements, i.e. 5, 6, 7 appear in both which make up the intersection of the two sets we saw above, they are therefore discarded. That’s symmetric difference for you.

In the next post we’ll look at some basic set operations from boolean set algebra.

Read all Python-related posts on this blog here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: