|
79 | 79 | del mySet
|
80 | 80 |
|
81 | 81 |
|
| 82 | +# difference() |
| 83 | + |
| 84 | +s1 = {1,2,3,4,5} |
| 85 | +s2 = {4,5,6,7,8} |
| 86 | + |
| 87 | +s1.difference(s2) #{1,2,3} |
| 88 | +s2.difference(s1) # {6,7,8} |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +# symmentric_difference() |
| 93 | +s1 = {1,2,3,4,5} |
| 94 | +s2 = {4,5,6,7,8} |
| 95 | + |
| 96 | +s1.symmentric_difference(s2) #{1,2,3,6,7,8} |
| 97 | +s2.symmentric_difference(s1) #{1,2,3,6,7,8} |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +# difference_update() Removes the items in this set that are also included in another, specified set |
| 102 | + |
| 103 | +s1.difference_update(s2) # {1,2,3} -> it actually remvoved 4,5 from s1 |
| 104 | + |
| 105 | +s2.difference_update(s1) # {6,7,8} --> it actually removed 4,5 from s2 |
| 106 | + |
| 107 | + |
| 108 | +# Union |
| 109 | + |
| 110 | +s1 = {1,2,3,4,5} |
| 111 | +s2 = {4,5,6,7,8} |
| 112 | + |
| 113 | +s1 | s2 # {1,2,3,4,5,6,7,8} --> the common items (4,5) between two sets are not repeated |
| 114 | + |
| 115 | + |
| 116 | +# Intersection |
| 117 | + |
| 118 | +s1 = {1,2,3,4,5} |
| 119 | +s2 = {4,5,6,7,8} |
| 120 | + |
| 121 | +s1 & s2 # {4, 5} --> returns common elements which are available in two sets |
| 122 | + |
| 123 | + |
| 124 | +# intersection_update() - Removes the items in this set that are not present in other, specified set(s) |
| 125 | + |
| 126 | +s1 = {1,2,3,4,5} |
| 127 | +s2 = {4,5,6,7,8} |
| 128 | + |
| 129 | +s1.intersection_update(s2) |
| 130 | +s1 # {4,5} because it removed not matching elements |
| 131 | +s2 # {8, 4, 5, 6, 7} its still the same |
| 132 | + |
| 133 | +s1 = {1,2,3,4,5} |
| 134 | +s2 = {4,5,6,7,8} |
| 135 | + |
| 136 | +s2.intersection_update(s1) |
| 137 | +s1 # {1,2,3,4,5} |
| 138 | +s2 # {4,5} because it removed not matching elements |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
0 commit comments