Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="module-itertools.html" /> | |
13 | <link rel="prev" href="module-array.html" /> | |
14 | <link rel="parent" href="misc.html" /> | |
15 | <link rel="next" href="set-objects.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>5.15 sets -- Unordered collections of unique elements</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="5.14 array " | |
25 | href="module-array.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services" | |
28 | href="misc.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="5.15.1 Set Objects" | |
31 | href="set-objects.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="module-array.html">5.14 array </A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="set-objects.html">5.15.1 Set Objects</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION0071500000000000000000"> | |
56 | 5.15 <tt class="module">sets</tt> -- | |
57 | Unordered collections of unique elements</A> | |
58 | </H1> | |
59 | ||
60 | <P> | |
61 | <A NAME="module-sets"></A> | |
62 | ||
63 | <P> | |
64 | ||
65 | <span class="versionnote">New in version 2.3.</span> | |
66 | ||
67 | <P> | |
68 | The <tt class="module">sets</tt> module provides classes for constructing and manipulating | |
69 | unordered collections of unique elements. Common uses include membership | |
70 | testing, removing duplicates from a sequence, and computing standard math | |
71 | operations on sets such as intersection, union, difference, and symmetric | |
72 | difference. | |
73 | ||
74 | <P> | |
75 | Like other collections, sets support <code><var>x</var> in <var>set</var></code>, | |
76 | <code>len(<var>set</var>)</code>, and <code>for <var>x</var> in <var>set</var></code>. Being an | |
77 | unordered collection, sets do not record element position or order of | |
78 | insertion. Accordingly, sets do not support indexing, slicing, or | |
79 | other sequence-like behavior. | |
80 | ||
81 | <P> | |
82 | Most set applications use the <tt class="class">Set</tt> class which provides every set | |
83 | method except for <tt class="method">__hash__()</tt>. For advanced applications requiring | |
84 | a hash method, the <tt class="class">ImmutableSet</tt> class adds a <tt class="method">__hash__()</tt> | |
85 | method but omits methods which alter the contents of the set. Both | |
86 | <tt class="class">Set</tt> and <tt class="class">ImmutableSet</tt> derive from <tt class="class">BaseSet</tt>, an | |
87 | abstract class useful for determining whether something is a set: | |
88 | <code>isinstance(<var>obj</var>, BaseSet)</code>. | |
89 | ||
90 | <P> | |
91 | The set classes are implemented using dictionaries. Accordingly, the | |
92 | requirements for set elements are the same as those for dictionary keys; | |
93 | namely, that the element defines both <tt class="method">__eq__</tt> and <tt class="method">__hash__</tt>. | |
94 | As a result, sets | |
95 | cannot contain mutable elements such as lists or dictionaries. | |
96 | However, they can contain immutable collections such as tuples or | |
97 | instances of <tt class="class">ImmutableSet</tt>. For convenience in implementing | |
98 | sets of sets, inner sets are automatically converted to immutable | |
99 | form, for example, <code>Set([Set(['dog'])])</code> is transformed to | |
100 | <code>Set([ImmutableSet(['dog'])])</code>. | |
101 | ||
102 | <P> | |
103 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
104 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-1380' xml:id='l2h-1380' class="class">Set</tt></b>(</nobr></td> | |
105 | <td><var></var><big>[</big><var>iterable</var><big>]</big><var></var>)</td></tr></table></dt> | |
106 | <dd> | |
107 | Constructs a new empty <tt class="class">Set</tt> object. If the optional <var>iterable</var> | |
108 | parameter is supplied, updates the set with elements obtained from iteration. | |
109 | All of the elements in <var>iterable</var> should be immutable or be transformable | |
110 | to an immutable using the protocol described in | |
111 | section <A href="immutable-transforms.html#immutable-transforms">5.15.3</A>. | |
112 | </dl> | |
113 | ||
114 | <P> | |
115 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
116 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-1381' xml:id='l2h-1381' class="class">ImmutableSet</tt></b>(</nobr></td> | |
117 | <td><var></var><big>[</big><var>iterable</var><big>]</big><var></var>)</td></tr></table></dt> | |
118 | <dd> | |
119 | Constructs a new empty <tt class="class">ImmutableSet</tt> object. If the optional | |
120 | <var>iterable</var> parameter is supplied, updates the set with elements obtained | |
121 | from iteration. All of the elements in <var>iterable</var> should be immutable or | |
122 | be transformable to an immutable using the protocol described in | |
123 | section <A href="immutable-transforms.html#immutable-transforms">5.15.3</A>. | |
124 | ||
125 | <P> | |
126 | Because <tt class="class">ImmutableSet</tt> objects provide a <tt class="method">__hash__()</tt> method, | |
127 | they can be used as set elements or as dictionary keys. <tt class="class">ImmutableSet</tt> | |
128 | objects do not have methods for adding or removing elements, so all of the | |
129 | elements must be known when the constructor is called. | |
130 | </dl> | |
131 | ||
132 | <P> | |
133 | ||
134 | <p><br /></p><hr class='online-navigation' /> | |
135 | <div class='online-navigation'> | |
136 | <!--Table of Child-Links--> | |
137 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
138 | ||
139 | <UL CLASS="ChildLinks"> | |
140 | <LI><A href="set-objects.html">5.15.1 Set Objects</a> | |
141 | <LI><A href="set-example.html">5.15.2 Example</a> | |
142 | <LI><A href="immutable-transforms.html">5.15.3 Protocol for automatic conversion to immutable</a> | |
143 | <LI><A href="comparison-to-builtin-set.html">5.15.4 Comparison to the built-in <tt class="class">set</tt> types</a> | |
144 | </ul> | |
145 | <!--End of Table of Child-Links--> | |
146 | </div> | |
147 | ||
148 | <DIV CLASS="navigation"> | |
149 | <div class='online-navigation'> | |
150 | <p></p><hr /> | |
151 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
152 | <tr> | |
153 | <td class='online-navigation'><a rel="prev" title="5.14 array " | |
154 | href="module-array.html"><img src='../icons/previous.png' | |
155 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
156 | <td class='online-navigation'><a rel="parent" title="5. Miscellaneous Services" | |
157 | href="misc.html"><img src='../icons/up.png' | |
158 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
159 | <td class='online-navigation'><a rel="next" title="5.15.1 Set Objects" | |
160 | href="set-objects.html"><img src='../icons/next.png' | |
161 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
162 | <td align="center" width="100%">Python Library Reference</td> | |
163 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
164 | href="contents.html"><img src='../icons/contents.png' | |
165 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
166 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
167 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
168 | <td class='online-navigation'><a rel="index" title="Index" | |
169 | href="genindex.html"><img src='../icons/index.png' | |
170 | border='0' height='32' alt='Index' width='32' /></A></td> | |
171 | </tr></table> | |
172 | <div class='online-navigation'> | |
173 | <b class="navlabel">Previous:</b> | |
174 | <a class="sectref" rel="prev" href="module-array.html">5.14 array </A> | |
175 | <b class="navlabel">Up:</b> | |
176 | <a class="sectref" rel="parent" href="misc.html">5. Miscellaneous Services</A> | |
177 | <b class="navlabel">Next:</b> | |
178 | <a class="sectref" rel="next" href="set-objects.html">5.15.1 Set Objects</A> | |
179 | </div> | |
180 | </div> | |
181 | <hr /> | |
182 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
183 | </DIV> | |
184 | <!--End of Navigation Panel--> | |
185 | <ADDRESS> | |
186 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
187 | </ADDRESS> | |
188 | </BODY> | |
189 | </HTML> |