Which module is "set " in?

Michael Hartl mhartl at post.harvard.edu
Sat Feb 26 01:58:16 EST 2005


It's good that you're using Python 2.3, which does have sets available,
as a previous poster mentioned.  Users of Python 2.2 or earlier can get
most of the Set functionality using the following class (from Peter
Norvig's utils.py file):

# class Set:
#     """This implements the Set class from PEP 218, except it does not
#     overload the infix operators.
#     Ex: s = Set([1,2,3]); 1 in s ==> True; 4 in s ==> False
#     s.add(4); 4 in s ==> True; len(s) ==> 4
#     s.discard(999); s.remove(4); 4 in s ==> False
#     s2 = Set([3,4,5]); s.union(s2) ==> Set([1,2,3,4,5])
#     s.intersection(s2) ==> Set([3])
#     Set([1,2,3]) ==> Set([3,2,1]); repr(s) ==> '{1, 2, 3}'
#     for e in s: pass"""
#
#     def __init__(self, elements=[]):
#         self.dict = {}
#         for e in elements:
#             self.dict[e] = 1
#
#     def __contains__(self, element):
#         return element in self.dict
#
#     def __getitem__(self, i):
#         return self.dict.items()[i]
#
#     def add(self, element):
#         self.dict[element] = 1
#
#     def remove(self, element):
#         del self.dict[element]
#
#     def discard(self, element):
#         if element in self.dict:
#             del self.dict[element]
#
#     def pop(self):
#         key, val = self.dict.popitem()
#         return key
#
#     def clear(self):
#         self.dict.clear()
#
#     def union(self, other):
#         return Set(self).union_update(other)
#
#     def intersection(self, other):
#         return Set(self).intersection_update(other)
#
#     def union_update(self, other):
#         for e in other:
#             self.add(e)
#         return self
#
#     def intersection_update(self, other):
#         for e in self.dict.keys():
#             if e not in other:
#                 self.remove(e)
#         return self
#
#     def issubset(self, other):
#         for e in self.dict.keys():
#             if e not in other:
#                 return False
#         return True
#
#     def __iter__(self):
#         for e in self.dict:
#             yield e
#
#     def __len__(self):
#         return len(self.dict)
#
#     def __cmp__(self, other):
#         if self is other: return False
#         if not isinstance(other, Set): return id(self) - id(other)
#         return cmp(self.dict, other.dict)
#
#     def __repr__(self):
#         return "{%s}" % ", ".join([str(e) for e in self.dict.keys()])

Michael

--
Michael D. Hartl, Ph.D.
CTO, Quark Sports LLC
http://quarksports.com/




More information about the Python-list mailing list