Harold Nelson
11/14/2019
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 “-”.
First define some trivial sets.
Try two different ways to create the union and compare the results.
## {1, 2, 3, 4, 5, 6}
## {1, 2, 3, 4, 5, 6}
Now check intersection.
## {3, 4}
## {3, 4}
Finally, the difference operator.
## {1, 2}
## {1, 2}
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.
## {5, 6, 7, 8, 9, 10}
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.
## {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.
## {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}
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.
## {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}
## [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]
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.
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
## [35, 70, 105, 140, 175]
## Union
## [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]
Create the set Universe as the set of all integers between 1 and 200.
## [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]
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.
## [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]