def union(self, other):
res = self.data[:] # make a copy of my list
for x in other:
if not x in res:
res.append(x)
return Set(res)
def concat(self, value): # value: a list, string, Set...
for x in value: # filters out duplicates
if not x in self.data:
self.data.append(x)
def len(self): return len(self.data)
def getitem(self, key): return self.data[key]
def and(self, other): return self.intersect(other)
def or(self, other): return self.union(other)
def repr(self): return '<Set:' + repr(self.data) + '>'
The Set class is used like the Stack class we saw earlier in this chapter: we make instances
and apply sequence operators plus unique set operations to them. Intersection and
union can be called as methods, or by using the & and | operators normally used for
built-in integer objects. Because we can string operators in expressions now (e.g., x &
y & z), there is no obvious need to support multiple operands in intersect/union
methods here (though this model’s need to create temporary objects within expressions
might eventually come to bear on performance). As with all rightly packaged objects,
we can either use the Set class within a program or test it interactively as follows:
>>> from set import Set
>>> users1 = Set(['Bob', 'Emily', 'Howard', 'Peeper'])
>>> users2 = Set(['Jerry', 'Howard', 'Carol'])
>>> users3 = Set(['Emily', 'Carol'])
>>> users1 & users2
<Set:['Howard']>
>>> users1 | users2
<Set:['Bob', 'Emily', 'Howard', 'Peeper', 'Jerry', 'Carol']>
>>> users1 | users2 & users3
<Set:['Bob', 'Emily', 'Howard', 'Peeper', 'Carol']>
>>> (users1 | users2) & users3
<Set:['Emily', 'Carol']>
>>> users1.data
['Bob', 'Emily', 'Howard', 'Peeper']
Optimization: Moving Sets to Dictionaries
Once you start using the Set class, the first problem you might encounter is its per-
formance: its nested for loops and in scans become exponentially slow. That slowness
may or may not be significant in your applications, but library classes should generally
be coded as efficiently as possible.
One way to optimize set performance is by changing the implementation to use dic-
tionaries rather than lists for storing sets internally—items may be stored as the keys
1378 | Chapter 18: Data Structures