|
40 | 40 | a particular new feature. |
41 | 41 |
|
42 | 42 |
|
| 43 | +%====================================================================== |
| 44 | +\section{PEP 218: A Standard Set Datatype} |
| 45 | + |
| 46 | +The new \module{sets} module contains an implementation of a set |
| 47 | +datatype. The \class{Set} class is for mutable sets, sets that can |
| 48 | +have members added and removed. The \class{ImmutableSet} class is for |
| 49 | +sets that can't be modified, and can be used as dictionary keys. Sets |
| 50 | +are built on top of dictionaries, so the elements within a set must be |
| 51 | +hashable. |
| 52 | + |
| 53 | +As a simple example, |
| 54 | + |
| 55 | +\begin{verbatim} |
| 56 | +>>> import sets |
| 57 | +>>> S = sets.Set([1,2,3]) |
| 58 | +>>> S |
| 59 | +Set([1, 2, 3]) |
| 60 | +>>> 1 in S |
| 61 | +True |
| 62 | +>>> 0 in S |
| 63 | +False |
| 64 | +>>> S.add(5) |
| 65 | +>>> S.remove(3) |
| 66 | +>>> S |
| 67 | +Set([1, 2, 5]) |
| 68 | +>>> |
| 69 | +\end{verbatim} |
| 70 | + |
| 71 | +The union and intersection of sets can be computed with the |
| 72 | +\method{union()} and \method{intersection()} methods, or, |
| 73 | +alternatively, using the bitwise operators \samp{\&} and \samp{|}. |
| 74 | +Mutable sets also have in-place versions of these methods, |
| 75 | +\method{union_update()} and \method{intersection_update()}. |
| 76 | + |
| 77 | +\begin{verbatim} |
| 78 | +>>> S1 = sets.Set([1,2,3]) |
| 79 | +>>> S2 = sets.Set([4,5,6]) |
| 80 | +>>> S1.union(S2) |
| 81 | +Set([1, 2, 3, 4, 5, 6]) |
| 82 | +>>> S1 | S2 # Alternative notation |
| 83 | +Set([1, 2, 3, 4, 5, 6]) |
| 84 | +>>> S1.intersection(S2) |
| 85 | +Set([]) |
| 86 | +>>> S1 & S2 # Alternative notation |
| 87 | +Set([]) |
| 88 | +>>> S1.union_update(S2) |
| 89 | +Set([1, 2, 3, 4, 5, 6]) |
| 90 | +>>> S1 |
| 91 | +Set([1, 2, 3, 4, 5, 6]) |
| 92 | +>>> |
| 93 | +\end{verbatim} |
| 94 | + |
| 95 | +It's also possible to take the symmetric difference of two sets. This |
| 96 | +is the set of all elements in the union that aren't in the |
| 97 | +intersection. An alternative way of expressing the symmetric |
| 98 | +difference is that it contains all elements that are in exactly one |
| 99 | +set. Again, there's an in-place version, with the ungainly name |
| 100 | +\method{symmetric_difference_update()}. |
| 101 | + |
| 102 | +\begin{verbatim} |
| 103 | +>>> S1 = sets.Set([1,2,3,4]) |
| 104 | +>>> S2 = sets.Set([3,4,5,6]) |
| 105 | +>>> S1.symmetric_difference(S2) |
| 106 | +Set([1, 2, 5, 6]) |
| 107 | +>>> S1 ^ S2 |
| 108 | +Set([1, 2, 5, 6]) |
| 109 | +>>> |
| 110 | +\end{verbatim} |
| 111 | + |
| 112 | +There are also methods, \method{issubset()} and \method{issuperset()}, |
| 113 | +for checking whether one set is a strict subset or superset of |
| 114 | +another: |
| 115 | + |
| 116 | +\begin{verbatim} |
| 117 | +>>> S1 = sets.Set([1,2,3]) |
| 118 | +>>> S2 = sets.Set([2,3]) |
| 119 | +>>> S2.issubset(S1) |
| 120 | +True |
| 121 | +>>> S1.issubset(S2) |
| 122 | +False |
| 123 | +>>> S1.issuperset(S2) |
| 124 | +True |
| 125 | +>>> |
| 126 | +\end{verbatim} |
| 127 | + |
| 128 | + |
| 129 | +\begin{seealso} |
| 130 | + |
| 131 | +\seepep{218}{Adding a Built-In Set Object Type}{PEP written by Greg V. Wilson. |
| 132 | +Implemented by Greg V. Wilson, Alex Martelli, and GvR.} |
| 133 | + |
| 134 | +\end{seealso} |
| 135 | + |
| 136 | + |
| 137 | + |
43 | 138 | %====================================================================== |
44 | 139 | \section{PEP 255: Simple Generators\label{section-generators}} |
45 | 140 |
|
|
0 commit comments