Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / lib / types-set.html
CommitLineData
920dae64
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="typesmapping.html" />
13<link rel="prev" href="typesseq.html" />
14<link rel="parent" href="types.html" />
15<link rel="next" href="typesmapping.html" />
16<meta name='aesop' content='information' />
17<title>2.3.7 Set Types -- set, frozenset </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="2.3.6.4 Mutable Sequence Types"
25 href="typesseq-mutable.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="2.3 Built-in Types"
28 href="types.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="2.3.8 Mapping Types "
31 href="typesmapping.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="typesseq-mutable.html">2.3.6.4 Mutable Sequence Types</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="types.html">2.3 Built-in Types</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="typesmapping.html">2.3.8 Mapping Types </A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H2><A NAME="SECTION004370000000000000000"></A><A NAME="types-set"></A>
56<BR>
572.3.7 Set Types --
58 <tt class="class">set</tt>, <tt class="class">frozenset</tt>
59
60</H2>
61<a id='l2h-226' xml:id='l2h-226'></a>
62<P>
63A <i class="dfn">set</i> object is an unordered collection of immutable values.
64Common uses include membership testing, removing duplicates from a sequence,
65and computing mathematical operations such as intersection, union, difference,
66and symmetric difference.
67
68<span class="versionnote">New in version 2.4.</span>
69
70<P>
71Like other collections, sets support <code><var>x</var> in <var>set</var></code>,
72<code>len(<var>set</var>)</code>, and <code>for <var>x</var> in <var>set</var></code>. Being an
73unordered collection, sets do not record element position or order of
74insertion. Accordingly, sets do not support indexing, slicing, or
75other sequence-like behavior.
76
77<P>
78There are currently two builtin set types, <tt class="class">set</tt> and <tt class="class">frozenset</tt>.
79The <tt class="class">set</tt> type is mutable -- the contents can be changed using methods
80like <tt class="method">add()</tt> and <tt class="method">remove()</tt>. Since it is mutable, it has no
81hash value and cannot be used as either a dictionary key or as an element of
82another set. The <tt class="class">frozenset</tt> type is immutable and hashable -- its
83contents cannot be altered after is created; however, it can be used as
84a dictionary key or as an element of another set.
85
86<P>
87Instances of <tt class="class">set</tt> and <tt class="class">frozenset</tt> provide the following operations:
88
89<P>
90<div class="center"><table class="realtable">
91 <thead>
92 <tr>
93 <th class="center">Operation</th>
94 <th class="center">Equivalent</th>
95 <th class="left" >Result</th>
96 </tr>
97 </thead>
98 <tbody>
99 <tr><td class="center" valign="baseline"><code>len(<var>s</var>)</code></td>
100 <td class="center"></td>
101 <td class="left" >cardinality of set <var>s</var></td></tr><P>
102
103 <tr><td class="center" valign="baseline"><code><var>x</var> in <var>s</var></code></td>
104 <td class="center"></td>
105 <td class="left" >test <var>x</var> for membership in <var>s</var></td></tr>
106 <tr><td class="center" valign="baseline"><code><var>x</var> not in <var>s</var></code></td>
107 <td class="center"></td>
108 <td class="left" >test <var>x</var> for non-membership in <var>s</var></td></tr>
109 <tr><td class="center" valign="baseline"><code><var>s</var>.issubset(<var>t</var>)</code></td>
110 <td class="center"><code><var>s</var> &lt;= <var>t</var></code></td>
111 <td class="left" >test whether every element in <var>s</var> is in <var>t</var></td></tr>
112 <tr><td class="center" valign="baseline"><code><var>s</var>.issuperset(<var>t</var>)</code></td>
113 <td class="center"><code><var>s</var> &gt;= <var>t</var></code></td>
114 <td class="left" >test whether every element in <var>t</var> is in <var>s</var></td></tr><P>
115
116 <tr><td class="center" valign="baseline"><code><var>s</var>.union(<var>t</var>)</code></td>
117 <td class="center"><var>s</var> | <var>t</var></td>
118 <td class="left" >new set with elements from both <var>s</var> and <var>t</var></td></tr>
119 <tr><td class="center" valign="baseline"><code><var>s</var>.intersection(<var>t</var>)</code></td>
120 <td class="center"><var>s</var> &amp; <var>t</var></td>
121 <td class="left" >new set with elements common to <var>s</var> and <var>t</var></td></tr>
122 <tr><td class="center" valign="baseline"><code><var>s</var>.difference(<var>t</var>)</code></td>
123 <td class="center"><var>s</var> - <var>t</var></td>
124 <td class="left" >new set with elements in <var>s</var> but not in <var>t</var></td></tr>
125 <tr><td class="center" valign="baseline"><code><var>s</var>.symmetric_difference(<var>t</var>)</code></td>
126 <td class="center"><var>s</var> ^ <var>t</var></td>
127 <td class="left" >new set with elements in either <var>s</var> or <var>t</var> but not both</td></tr>
128 <tr><td class="center" valign="baseline"><code><var>s</var>.copy()</code></td>
129 <td class="center"></td>
130 <td class="left" >new set with a shallow copy of <var>s</var></td></tr></tbody>
131</table></div>
132
133<P>
134Note, the non-operator versions of <tt class="method">union()</tt>, <tt class="method">intersection()</tt>,
135<tt class="method">difference()</tt>, and <tt class="method">symmetric_difference()</tt>,
136<tt class="method">issubset()</tt>, and <tt class="method">issuperset()</tt> methods will accept any
137iterable as an argument. In contrast, their operator based counterparts
138require their arguments to be sets. This precludes error-prone constructions
139like <code>set('abc') &amp; 'cbs'</code> in favor of the more readable
140<code>set('abc').intersection('cbs')</code>.
141
142<P>
143Both <tt class="class">set</tt> and <tt class="class">frozenset</tt> support set to set comparisons.
144Two sets are equal if and only if every element of each set is contained in
145the other (each is a subset of the other).
146A set is less than another set if and only if the first set is a proper
147subset of the second set (is a subset, but is not equal).
148A set is greater than another set if and only if the first set is a proper
149superset of the second set (is a superset, but is not equal).
150
151<P>
152Instances of <tt class="class">set</tt> are compared to instances of <tt class="class">frozenset</tt> based
153on their members. For example, "<tt class="samp">set('abc') == frozenset('abc')</tt>" returns
154<code>True</code>.
155
156<P>
157The subset and equality comparisons do not generalize to a complete
158ordering function. For example, any two disjoint sets are not equal and
159are not subsets of each other, so <em>all</em> of the following return
160<code>False</code>: <code><var>a</var>&lt;<var>b</var></code>, <code><var>a</var>==<var>b</var></code>, or
161<code><var>a</var>&gt;<var>b</var></code>.
162Accordingly, sets do not implement the <tt class="method">__cmp__</tt> method.
163
164<P>
165Since sets only define partial ordering (subset relationships), the output
166of the <tt class="method">list.sort()</tt> method is undefined for lists of sets.
167
168<P>
169Set elements are like dictionary keys; they need to define both
170<tt class="method">__hash__</tt> and <tt class="method">__eq__</tt> methods.
171
172<P>
173Binary operations that mix <tt class="class">set</tt> instances with <tt class="class">frozenset</tt>
174return the type of the first operand. For example:
175"<tt class="samp">frozenset('ab') | set('bc')</tt>" returns an instance of <tt class="class">frozenset</tt>.
176
177<P>
178The following table lists operations available for <tt class="class">set</tt>
179that do not apply to immutable instances of <tt class="class">frozenset</tt>:
180
181<P>
182<div class="center"><table class="realtable">
183 <thead>
184 <tr>
185 <th class="center">Operation</th>
186 <th class="center">Equivalent</th>
187 <th class="left" >Result</th>
188 </tr>
189 </thead>
190 <tbody>
191 <tr><td class="center" valign="baseline"><code><var>s</var>.update(<var>t</var>)</code></td>
192 <td class="center"><var>s</var> |= <var>t</var></td>
193 <td class="left" >return set <var>s</var> with elements added from <var>t</var></td></tr>
194 <tr><td class="center" valign="baseline"><code><var>s</var>.intersection_update(<var>t</var>)</code></td>
195 <td class="center"><var>s</var> &amp;= <var>t</var></td>
196 <td class="left" >return set <var>s</var> keeping only elements also found in <var>t</var></td></tr>
197 <tr><td class="center" valign="baseline"><code><var>s</var>.difference_update(<var>t</var>)</code></td>
198 <td class="center"><var>s</var> -= <var>t</var></td>
199 <td class="left" >return set <var>s</var> after removing elements found in <var>t</var></td></tr>
200 <tr><td class="center" valign="baseline"><code><var>s</var>.symmetric_difference_update(<var>t</var>)</code></td>
201 <td class="center"><var>s</var> ^= <var>t</var></td>
202 <td class="left" >return set <var>s</var> with elements from <var>s</var> or <var>t</var>
203 but not both</td></tr><P>
204
205 <tr><td class="center" valign="baseline"><code><var>s</var>.add(<var>x</var>)</code></td>
206 <td class="center"></td>
207 <td class="left" >add element <var>x</var> to set <var>s</var></td></tr>
208 <tr><td class="center" valign="baseline"><code><var>s</var>.remove(<var>x</var>)</code></td>
209 <td class="center"></td>
210 <td class="left" >remove <var>x</var> from set <var>s</var>; raises KeyError if not present</td></tr>
211 <tr><td class="center" valign="baseline"><code><var>s</var>.discard(<var>x</var>)</code></td>
212 <td class="center"></td>
213 <td class="left" >removes <var>x</var> from set <var>s</var> if present</td></tr>
214 <tr><td class="center" valign="baseline"><code><var>s</var>.pop()</code></td>
215 <td class="center"></td>
216 <td class="left" >remove and return an arbitrary element from <var>s</var>; raises
217 <tt class="exception">KeyError</tt> if empty</td></tr>
218 <tr><td class="center" valign="baseline"><code><var>s</var>.clear()</code></td>
219 <td class="center"></td>
220 <td class="left" >remove all elements from set <var>s</var></td></tr></tbody>
221</table></div>
222
223<P>
224Note, the non-operator versions of the <tt class="method">update()</tt>,
225<tt class="method">intersection_update()</tt>, <tt class="method">difference_update()</tt>, and
226<tt class="method">symmetric_difference_update()</tt> methods will accept any iterable
227as an argument.
228
229<P>
230The design of the set types was based on lessons learned from the
231<tt class="module">sets</tt> module.
232
233<P>
234<div class="seealso">
235 <p class="heading">See Also:</p>
236
237 <dl compact="compact" class="seemodule">
238 <dt>Module <b><tt class="module"><a href="module-comparison-to-builtin-set.html">sets</a></tt>:</b>
239 <dd>Differences between
240 the <tt class="module">sets</tt> module and the built-in set types.
241 </dl>
242</div>
243
244<P>
245
246<DIV CLASS="navigation">
247<div class='online-navigation'>
248<p></p><hr />
249<table align="center" width="100%" cellpadding="0" cellspacing="2">
250<tr>
251<td class='online-navigation'><a rel="prev" title="2.3.6.4 Mutable Sequence Types"
252 href="typesseq-mutable.html"><img src='../icons/previous.png'
253 border='0' height='32' alt='Previous Page' width='32' /></A></td>
254<td class='online-navigation'><a rel="parent" title="2.3 Built-in Types"
255 href="types.html"><img src='../icons/up.png'
256 border='0' height='32' alt='Up One Level' width='32' /></A></td>
257<td class='online-navigation'><a rel="next" title="2.3.8 Mapping Types "
258 href="typesmapping.html"><img src='../icons/next.png'
259 border='0' height='32' alt='Next Page' width='32' /></A></td>
260<td align="center" width="100%">Python Library Reference</td>
261<td class='online-navigation'><a rel="contents" title="Table of Contents"
262 href="contents.html"><img src='../icons/contents.png'
263 border='0' height='32' alt='Contents' width='32' /></A></td>
264<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
265 border='0' height='32' alt='Module Index' width='32' /></a></td>
266<td class='online-navigation'><a rel="index" title="Index"
267 href="genindex.html"><img src='../icons/index.png'
268 border='0' height='32' alt='Index' width='32' /></A></td>
269</tr></table>
270<div class='online-navigation'>
271<b class="navlabel">Previous:</b>
272<a class="sectref" rel="prev" href="typesseq-mutable.html">2.3.6.4 Mutable Sequence Types</A>
273<b class="navlabel">Up:</b>
274<a class="sectref" rel="parent" href="types.html">2.3 Built-in Types</A>
275<b class="navlabel">Next:</b>
276<a class="sectref" rel="next" href="typesmapping.html">2.3.8 Mapping Types </A>
277</div>
278</div>
279<hr />
280<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
281</DIV>
282<!--End of Navigation Panel-->
283<ADDRESS>
284See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
285</ADDRESS>
286</BODY>
287</HTML>