/Doc/library/sets.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 286 lines · 227 code · 59 blank · 0 comment · 0 complexity · e7fa38fe0907b85d9e8d8c291f9a3475 MD5 · raw file

  1. :mod:`sets` --- Unordered collections of unique elements
  2. ========================================================
  3. .. module:: sets
  4. :synopsis: Implementation of sets of unique elements.
  5. :deprecated:
  6. .. moduleauthor:: Greg V. Wilson <gvwilson@nevex.com>
  7. .. moduleauthor:: Alex Martelli <aleax@aleax.it>
  8. .. moduleauthor:: Guido van Rossum <guido@python.org>
  9. .. sectionauthor:: Raymond D. Hettinger <python@rcn.com>
  10. .. versionadded:: 2.3
  11. .. deprecated:: 2.6
  12. The built-in ``set``/``frozenset`` types replace this module.
  13. The :mod:`sets` module provides classes for constructing and manipulating
  14. unordered collections of unique elements. Common uses include membership
  15. testing, removing duplicates from a sequence, and computing standard math
  16. operations on sets such as intersection, union, difference, and symmetric
  17. difference.
  18. Like other collections, sets support ``x in set``, ``len(set)``, and ``for x in
  19. set``. Being an unordered collection, sets do not record element position or
  20. order of insertion. Accordingly, sets do not support indexing, slicing, or
  21. other sequence-like behavior.
  22. Most set applications use the :class:`Set` class which provides every set method
  23. except for :meth:`__hash__`. For advanced applications requiring a hash method,
  24. the :class:`ImmutableSet` class adds a :meth:`__hash__` method but omits methods
  25. which alter the contents of the set. Both :class:`Set` and :class:`ImmutableSet`
  26. derive from :class:`BaseSet`, an abstract class useful for determining whether
  27. something is a set: ``isinstance(obj, BaseSet)``.
  28. The set classes are implemented using dictionaries. Accordingly, the
  29. requirements for set elements are the same as those for dictionary keys; namely,
  30. that the element defines both :meth:`__eq__` and :meth:`__hash__`. As a result,
  31. sets cannot contain mutable elements such as lists or dictionaries. However,
  32. they can contain immutable collections such as tuples or instances of
  33. :class:`ImmutableSet`. For convenience in implementing sets of sets, inner sets
  34. are automatically converted to immutable form, for example,
  35. ``Set([Set(['dog'])])`` is transformed to ``Set([ImmutableSet(['dog'])])``.
  36. .. class:: Set([iterable])
  37. Constructs a new empty :class:`Set` object. If the optional *iterable*
  38. parameter is supplied, updates the set with elements obtained from iteration.
  39. All of the elements in *iterable* should be immutable or be transformable to an
  40. immutable using the protocol described in section :ref:`immutable-transforms`.
  41. .. class:: ImmutableSet([iterable])
  42. Constructs a new empty :class:`ImmutableSet` object. If the optional *iterable*
  43. parameter is supplied, updates the set with elements obtained from iteration.
  44. All of the elements in *iterable* should be immutable or be transformable to an
  45. immutable using the protocol described in section :ref:`immutable-transforms`.
  46. Because :class:`ImmutableSet` objects provide a :meth:`__hash__` method, they
  47. can be used as set elements or as dictionary keys. :class:`ImmutableSet`
  48. objects do not have methods for adding or removing elements, so all of the
  49. elements must be known when the constructor is called.
  50. .. _set-objects:
  51. Set Objects
  52. -----------
  53. Instances of :class:`Set` and :class:`ImmutableSet` both provide the following
  54. operations:
  55. +-------------------------------+------------+---------------------------------+
  56. | Operation | Equivalent | Result |
  57. +===============================+============+=================================+
  58. | ``len(s)`` | | cardinality of set *s* |
  59. +-------------------------------+------------+---------------------------------+
  60. | ``x in s`` | | test *x* for membership in *s* |
  61. +-------------------------------+------------+---------------------------------+
  62. | ``x not in s`` | | test *x* for non-membership in |
  63. | | | *s* |
  64. +-------------------------------+------------+---------------------------------+
  65. | ``s.issubset(t)`` | ``s <= t`` | test whether every element in |
  66. | | | *s* is in *t* |
  67. +-------------------------------+------------+---------------------------------+
  68. | ``s.issuperset(t)`` | ``s >= t`` | test whether every element in |
  69. | | | *t* is in *s* |
  70. +-------------------------------+------------+---------------------------------+
  71. | ``s.union(t)`` | ``s | t`` | new set with elements from both |
  72. | | | *s* and *t* |
  73. +-------------------------------+------------+---------------------------------+
  74. | ``s.intersection(t)`` | ``s & t`` | new set with elements common to |
  75. | | | *s* and *t* |
  76. +-------------------------------+------------+---------------------------------+
  77. | ``s.difference(t)`` | ``s - t`` | new set with elements in *s* |
  78. | | | but not in *t* |
  79. +-------------------------------+------------+---------------------------------+
  80. | ``s.symmetric_difference(t)`` | ``s ^ t`` | new set with elements in either |
  81. | | | *s* or *t* but not both |
  82. +-------------------------------+------------+---------------------------------+
  83. | ``s.copy()`` | | new set with a shallow copy of |
  84. | | | *s* |
  85. +-------------------------------+------------+---------------------------------+
  86. Note, the non-operator versions of :meth:`union`, :meth:`intersection`,
  87. :meth:`difference`, and :meth:`symmetric_difference` will accept any iterable as
  88. an argument. In contrast, their operator based counterparts require their
  89. arguments to be sets. This precludes error-prone constructions like
  90. ``Set('abc') & 'cbs'`` in favor of the more readable
  91. ``Set('abc').intersection('cbs')``.
  92. .. versionchanged:: 2.3.1
  93. Formerly all arguments were required to be sets.
  94. In addition, both :class:`Set` and :class:`ImmutableSet` support set to set
  95. comparisons. Two sets are equal if and only if every element of each set is
  96. contained in the other (each is a subset of the other). A set is less than
  97. another set if and only if the first set is a proper subset of the second set
  98. (is a subset, but is not equal). A set is greater than another set if and only
  99. if the first set is a proper superset of the second set (is a superset, but is
  100. not equal).
  101. The subset and equality comparisons do not generalize to a complete ordering
  102. function. For example, any two disjoint sets are not equal and are not subsets
  103. of each other, so *all* of the following return ``False``: ``a<b``, ``a==b``,
  104. or ``a>b``. Accordingly, sets do not implement the :meth:`__cmp__` method.
  105. Since sets only define partial ordering (subset relationships), the output of
  106. the :meth:`list.sort` method is undefined for lists of sets.
  107. The following table lists operations available in :class:`ImmutableSet` but not
  108. found in :class:`Set`:
  109. +-------------+------------------------------+
  110. | Operation | Result |
  111. +=============+==============================+
  112. | ``hash(s)`` | returns a hash value for *s* |
  113. +-------------+------------------------------+
  114. The following table lists operations available in :class:`Set` but not found in
  115. :class:`ImmutableSet`:
  116. +--------------------------------------+-------------+---------------------------------+
  117. | Operation | Equivalent | Result |
  118. +======================================+=============+=================================+
  119. | ``s.update(t)`` | *s* \|= *t* | return set *s* with elements |
  120. | | | added from *t* |
  121. +--------------------------------------+-------------+---------------------------------+
  122. | ``s.intersection_update(t)`` | *s* &= *t* | return set *s* keeping only |
  123. | | | elements also found in *t* |
  124. +--------------------------------------+-------------+---------------------------------+
  125. | ``s.difference_update(t)`` | *s* -= *t* | return set *s* after removing |
  126. | | | elements found in *t* |
  127. +--------------------------------------+-------------+---------------------------------+
  128. | ``s.symmetric_difference_update(t)`` | *s* ^= *t* | return set *s* with elements |
  129. | | | from *s* or *t* but not both |
  130. +--------------------------------------+-------------+---------------------------------+
  131. | ``s.add(x)`` | | add element *x* to set *s* |
  132. +--------------------------------------+-------------+---------------------------------+
  133. | ``s.remove(x)`` | | remove *x* from set *s*; raises |
  134. | | | :exc:`KeyError` if not present |
  135. +--------------------------------------+-------------+---------------------------------+
  136. | ``s.discard(x)`` | | removes *x* from set *s* if |
  137. | | | present |
  138. +--------------------------------------+-------------+---------------------------------+
  139. | ``s.pop()`` | | remove and return an arbitrary |
  140. | | | element from *s*; raises |
  141. | | | :exc:`KeyError` if empty |
  142. +--------------------------------------+-------------+---------------------------------+
  143. | ``s.clear()`` | | remove all elements from set |
  144. | | | *s* |
  145. +--------------------------------------+-------------+---------------------------------+
  146. Note, the non-operator versions of :meth:`update`, :meth:`intersection_update`,
  147. :meth:`difference_update`, and :meth:`symmetric_difference_update` will accept
  148. any iterable as an argument.
  149. .. versionchanged:: 2.3.1
  150. Formerly all arguments were required to be sets.
  151. Also note, the module also includes a :meth:`union_update` method which is an
  152. alias for :meth:`update`. The method is included for backwards compatibility.
  153. Programmers should prefer the :meth:`update` method because it is supported by
  154. the builtin :class:`set()` and :class:`frozenset()` types.
  155. .. _set-example:
  156. Example
  157. -------
  158. >>> from sets import Set
  159. >>> engineers = Set(['John', 'Jane', 'Jack', 'Janice'])
  160. >>> programmers = Set(['Jack', 'Sam', 'Susan', 'Janice'])
  161. >>> managers = Set(['Jane', 'Jack', 'Susan', 'Zack'])
  162. >>> employees = engineers | programmers | managers # union
  163. >>> engineering_management = engineers & managers # intersection
  164. >>> fulltime_management = managers - engineers - programmers # difference
  165. >>> engineers.add('Marvin') # add element
  166. >>> print engineers # doctest: +SKIP
  167. Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])
  168. >>> employees.issuperset(engineers) # superset test
  169. False
  170. >>> employees.update(engineers) # update from another set
  171. >>> employees.issuperset(engineers)
  172. True
  173. >>> for group in [engineers, programmers, managers, employees]: # doctest: +SKIP
  174. ... group.discard('Susan') # unconditionally remove element
  175. ... print group
  176. ...
  177. Set(['Jane', 'Marvin', 'Janice', 'John', 'Jack'])
  178. Set(['Janice', 'Jack', 'Sam'])
  179. Set(['Jane', 'Zack', 'Jack'])
  180. Set(['Jack', 'Sam', 'Jane', 'Marvin', 'Janice', 'John', 'Zack'])
  181. .. _immutable-transforms:
  182. Protocol for automatic conversion to immutable
  183. ----------------------------------------------
  184. Sets can only contain immutable elements. For convenience, mutable :class:`Set`
  185. objects are automatically copied to an :class:`ImmutableSet` before being added
  186. as a set element.
  187. The mechanism is to always add a :term:`hashable` element, or if it is not
  188. hashable, the element is checked to see if it has an :meth:`__as_immutable__`
  189. method which returns an immutable equivalent.
  190. Since :class:`Set` objects have a :meth:`__as_immutable__` method returning an
  191. instance of :class:`ImmutableSet`, it is possible to construct sets of sets.
  192. A similar mechanism is needed by the :meth:`__contains__` and :meth:`remove`
  193. methods which need to hash an element to check for membership in a set. Those
  194. methods check an element for hashability and, if not, check for a
  195. :meth:`__as_temporarily_immutable__` method which returns the element wrapped by
  196. a class that provides temporary methods for :meth:`__hash__`, :meth:`__eq__`,
  197. and :meth:`__ne__`.
  198. The alternate mechanism spares the need to build a separate copy of the original
  199. mutable object.
  200. :class:`Set` objects implement the :meth:`__as_temporarily_immutable__` method
  201. which returns the :class:`Set` object wrapped by a new class
  202. :class:`_TemporarilyImmutableSet`.
  203. The two mechanisms for adding hashability are normally invisible to the user;
  204. however, a conflict can arise in a multi-threaded environment where one thread
  205. is updating a set while another has temporarily wrapped it in
  206. :class:`_TemporarilyImmutableSet`. In other words, sets of mutable sets are not
  207. thread-safe.
  208. .. _comparison-to-builtin-set:
  209. Comparison to the built-in :class:`set` types
  210. ---------------------------------------------
  211. The built-in :class:`set` and :class:`frozenset` types were designed based on
  212. lessons learned from the :mod:`sets` module. The key differences are:
  213. * :class:`Set` and :class:`ImmutableSet` were renamed to :class:`set` and
  214. :class:`frozenset`.
  215. * There is no equivalent to :class:`BaseSet`. Instead, use ``isinstance(x,
  216. (set, frozenset))``.
  217. * The hash algorithm for the built-ins performs significantly better (fewer
  218. collisions) for most datasets.
  219. * The built-in versions have more space efficient pickles.
  220. * The built-in versions do not have a :meth:`union_update` method. Instead, use
  221. the :meth:`update` method which is equivalent.
  222. * The built-in versions do not have a ``_repr(sorted=True)`` method.
  223. Instead, use the built-in :func:`repr` and :func:`sorted` functions:
  224. ``repr(sorted(s))``.
  225. * The built-in version does not have a protocol for automatic conversion to
  226. immutable. Many found this feature to be confusing and no one in the community
  227. reported having found real uses for it.