The sets module provides classes for constructing and manipulating unordered collections of unique elements.
engineers = Set(['John', 'Jane', 'Jack', 'Janice'])
programmers = Set(['Jack', 'Sam', 'Susan', 'Janice'])
managers = Set(['Jane', 'Jack', 'Susan', 'Zack'])
employees = engineers | programmers | managers # union
engineering_management = engineers &am; managers # intersection
fulltime_management = managers - engineers - programmers # difference
engineers.add('Marvin') # add element
print engineers # Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])
employees.issuperset(engineers) # superset test
False
employees.union_update(engineers) # update from another set
employees.issuperset(engineers)
True
for group in [engineers, programmers, managers, employees]:
... group.discard('Susan') # unconditionally remove element
... print group
...
Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])
Set(['Janice', 'Jack', 'Sam'])
Set(['Jane', 'Zack', 'Jack'])
Set(['Jack', 'Sam', 'Jane', 'Marvin', 'Janice', 'John', 'Zack'])
-
s.symmetric_difference(t) - s ^ t - new set with elements in either s or t but not both.
Set([1,2,3]).symmetric_difference(Set([3,4,5])) # Set([1,2,4,5])
-
s.copy() - new set with a shallow copy of s.
s = Set([1,2,3])
s.copy() # Set([1, 2, 3])
-
s.discard(x) removes x from set s if present
s = Set([1,2,3])
s.discard(2) # Set([1,3])
-
s.pop() remove and return an arbitrary element from s; raises KeyError if empty
s = Set([1,2,3])
s.pop() # 3
s # Set([1,2])
-
s.clear() remove all elements from set s
s = Set([1,2,3])
s.clear()
s # Set([])
index
