Sets

Harold Nelson

11/14/2019

Operators

Most presentations on sets in Python mention only methods to implement intersection, union, and difference.

This makes the jump from mathematical notation awkward. There are also operators to implement these operations on sets. Union is implemented as the operator “|”. Intersection is implemented as “&”. Set difference is implemented as the standard difference operator “-”.

Examples

First define some trivial sets.

A = {1,2,3,4}
B = {3,4,5,6}

Try two different ways to create the union and compare the results.

U1 = A.union(B)
U2 = A | B
print(U1)
## {1, 2, 3, 4, 5, 6}
print(U2)
## {1, 2, 3, 4, 5, 6}

Now check intersection.

I1 = A.intersection(B)
I2 = A & B
print(I1)
## {3, 4}
print(I2)
## {3, 4}

Finally, the difference operator.

D1 = A.difference(B)
D2 = A - B
print(D1)
## {1, 2}
print(D2)
## {1, 2}

Complement

Mathematical discussions of set theory always incorporate the notion of a complement. The complement of the set A consists of all things not in the set A. Of course, this relies on a well-defined set of all possible objects, called the universe. Once the universe is defined, the complement of A is simply the difference between the universe and the set A.

It is easy to define a function in Python to play this role. Let’s take the universe as the set of integers between 1 and 10 and define the complement function.

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

def comp(x):
    return (Universe - x)

CA = comp(A)
print(CA)
## {5, 6, 7, 8, 9, 10}

Set Comprehensions

A set comprehension is a way of building a set which allows for a more complex construction, not feasible with a simple list.

We could create a set consisting of all of the integers in a range without a comprehension.

int_0_99 = set(range(100))
int_0_99
## {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99}

A comprehension allows us to be more manipulative and selective.

even_1_100 = {(i+1) for i in range(100) if (i+1)%2 == 0}
even_1_100
## {2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100}

Exercise

Write a set comprehension to produce the set of all integers between 1 and 200 evenly divisible by 7. Call it m_7_1_200. Also print the result as a sorted list.

Answer

m_7_1_200 = {i+1 for i in range(200) if (i+1)%7 == 0}
m_7_1_200
## {133, 7, 140, 14, 147, 21, 154, 28, 161, 35, 168, 42, 175, 49, 182, 56, 189, 63, 196, 70, 77, 84, 91, 98, 105, 112, 119, 126}
print(sorted(m_7_1_200))
## [7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98, 105, 112, 119, 126, 133, 140, 147, 154, 161, 168, 175, 182, 189, 196]

Exercise

Build the set m_5_1_200 following the pattern above and then create the intersection and the union of the two sets. Call the i_5_7 and u_5_7 respectively.

Answer

m_5_1_200 = {i+1 for i in range(200) if (i+1)%5 == 0}
i_5_7 = m_5_1_200 & m_7_1_200
u_5_7 = m_5_1_200 | m_7_1_200
print("Intersection")
## Intersection
print(sorted(i_5_7))
## [35, 70, 105, 140, 175]
print("")
print("Union")
## Union
print(sorted(u_5_7))
## [5, 7, 10, 14, 15, 20, 21, 25, 28, 30, 35, 40, 42, 45, 49, 50, 55, 56, 60, 63, 65, 70, 75, 77, 80, 84, 85, 90, 91, 95, 98, 100, 105, 110, 112, 115, 119, 120, 125, 126, 130, 133, 135, 140, 145, 147, 150, 154, 155, 160, 161, 165, 168, 170, 175, 180, 182, 185, 189, 190, 195, 196, 200]

Exercise

Create the set Universe as the set of all integers between 1 and 200.

Answer

Universe = {i+1 for i in range(200)}
sorted(Universe)
## [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200]

Exercise

Create the function comp based on this universe and use it to create the complement of the set u_5_7, called u_5_7_c.

Answer

def comp(x):
    return (Universe - x)
u_5_7_c = comp(u_5_7)
sorted(u_5_7_c)
    
## [1, 2, 3, 4, 6, 8, 9, 11, 12, 13, 16, 17, 18, 19, 22, 23, 24, 26, 27, 29, 31, 32, 33, 34, 36, 37, 38, 39, 41, 43, 44, 46, 47, 48, 51, 52, 53, 54, 57, 58, 59, 61, 62, 64, 66, 67, 68, 69, 71, 72, 73, 74, 76, 78, 79, 81, 82, 83, 86, 87, 88, 89, 92, 93, 94, 96, 97, 99, 101, 102, 103, 104, 106, 107, 108, 109, 111, 113, 114, 116, 117, 118, 121, 122, 123, 124, 127, 128, 129, 131, 132, 134, 136, 137, 138, 139, 141, 142, 143, 144, 146, 148, 149, 151, 152, 153, 156, 157, 158, 159, 162, 163, 164, 166, 167, 169, 171, 172, 173, 174, 176, 177, 178, 179, 181, 183, 184, 186, 187, 188, 191, 192, 193, 194, 197, 198, 199]