~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.12-docs-html/library/sets.html

  • Committer: Dave Kuhlman
  • Date: 2017-04-15 16:24:56 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20170415162456-iav9vozzg4iwqwv3
Updated docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
2
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
3
 
 
4
 
 
5
<html xmlns="http://www.w3.org/1999/xhtml">
 
6
  <head>
 
7
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 
8
    
 
9
    <title>8.7. sets — Unordered collections of unique elements &mdash; Python 2.7.12 documentation</title>
 
10
    
 
11
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
 
12
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
 
13
    
 
14
    <script type="text/javascript">
 
15
      var DOCUMENTATION_OPTIONS = {
 
16
        URL_ROOT:    '../',
 
17
        VERSION:     '2.7.12',
 
18
        COLLAPSE_INDEX: false,
 
19
        FILE_SUFFIX: '.html',
 
20
        HAS_SOURCE:  true
 
21
      };
 
22
    </script>
 
23
    <script type="text/javascript" src="../_static/jquery.js"></script>
 
24
    <script type="text/javascript" src="../_static/underscore.js"></script>
 
25
    <script type="text/javascript" src="../_static/doctools.js"></script>
 
26
    <script type="text/javascript" src="../_static/sidebar.js"></script>
 
27
    <link rel="search" type="application/opensearchdescription+xml"
 
28
          title="Search within Python 2.7.12 documentation"
 
29
          href="../_static/opensearch.xml"/>
 
30
    <link rel="author" title="About these documents" href="../about.html" />
 
31
    <link rel="copyright" title="Copyright" href="../copyright.html" />
 
32
    <link rel="top" title="Python 2.7.12 documentation" href="../contents.html" />
 
33
    <link rel="up" title="8. Data Types" href="datatypes.html" />
 
34
    <link rel="next" title="8.8. sched — Event scheduler" href="sched.html" />
 
35
    <link rel="prev" title="8.6. array — Efficient arrays of numeric values" href="array.html" />
 
36
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 
37
    <script type="text/javascript" src="../_static/copybutton.js"></script>
 
38
    <script type="text/javascript" src="../_static/version_switch.js"></script>
 
39
 
 
40
    
 
41
 
 
42
  </head>
 
43
  <body role="document">  
 
44
    <div class="related" role="navigation" aria-label="related navigation">
 
45
      <h3>Navigation</h3>
 
46
      <ul>
 
47
        <li class="right" style="margin-right: 10px">
 
48
          <a href="../genindex.html" title="General Index"
 
49
             accesskey="I">index</a></li>
 
50
        <li class="right" >
 
51
          <a href="../py-modindex.html" title="Python Module Index"
 
52
             >modules</a> |</li>
 
53
        <li class="right" >
 
54
          <a href="sched.html" title="8.8. sched — Event scheduler"
 
55
             accesskey="N">next</a> |</li>
 
56
        <li class="right" >
 
57
          <a href="array.html" title="8.6. array — Efficient arrays of numeric values"
 
58
             accesskey="P">previous</a> |</li>
 
59
        <li><img src="../_static/py.png" alt=""
 
60
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
61
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
62
        <li>
 
63
          <span class="version_switcher_placeholder">2.7.12</span>
 
64
          <a href="../index.html">Documentation</a> &raquo;
 
65
        </li>
 
66
 
 
67
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
68
          <li class="nav-item nav-item-2"><a href="datatypes.html" accesskey="U">8. Data Types</a> &raquo;</li> 
 
69
      </ul>
 
70
    </div>    
 
71
 
 
72
    <div class="document">
 
73
      <div class="documentwrapper">
 
74
        <div class="bodywrapper">
 
75
          <div class="body" role="main">
 
76
            
 
77
  <div class="section" id="module-sets">
 
78
<span id="sets-unordered-collections-of-unique-elements"></span><h1>8.7. <a class="reference internal" href="#module-sets" title="sets: Implementation of sets of unique elements. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">sets</span></code></a> &#8212; Unordered collections of unique elements<a class="headerlink" href="#module-sets" title="Permalink to this headline">¶</a></h1>
 
79
<div class="versionadded">
 
80
<p><span class="versionmodified">New in version 2.3.</span></p>
 
81
</div>
 
82
<div class="deprecated">
 
83
<p><span class="versionmodified">Deprecated since version 2.6: </span>The built-in <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-class docutils literal"><span class="pre">set</span></code></a>/<a class="reference internal" href="stdtypes.html#frozenset" title="frozenset"><code class="xref py py-class docutils literal"><span class="pre">frozenset</span></code></a> types replace this module.</p>
 
84
</div>
 
85
<p>The <a class="reference internal" href="#module-sets" title="sets: Implementation of sets of unique elements. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">sets</span></code></a> module provides classes for constructing and manipulating
 
86
unordered collections of unique elements.  Common uses include membership
 
87
testing, removing duplicates from a sequence, and computing standard math
 
88
operations on sets such as intersection, union, difference, and symmetric
 
89
difference.</p>
 
90
<p>Like other collections, sets support <code class="docutils literal"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">set</span></code>, <code class="docutils literal"><span class="pre">len(set)</span></code>, and <code class="docutils literal"><span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span>
 
91
<span class="pre">set</span></code>.  Being an unordered collection, sets do not record element position or
 
92
order of insertion.  Accordingly, sets do not support indexing, slicing, or
 
93
other sequence-like behavior.</p>
 
94
<p>Most set applications use the <a class="reference internal" href="#sets.Set" title="sets.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a> class which provides every set method
 
95
except for <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal"><span class="pre">__hash__()</span></code></a>. For advanced applications requiring a hash method,
 
96
the <a class="reference internal" href="#sets.ImmutableSet" title="sets.ImmutableSet"><code class="xref py py-class docutils literal"><span class="pre">ImmutableSet</span></code></a> class adds a <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal"><span class="pre">__hash__()</span></code></a> method but omits methods
 
97
which alter the contents of the set. Both <a class="reference internal" href="#sets.Set" title="sets.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a> and <a class="reference internal" href="#sets.ImmutableSet" title="sets.ImmutableSet"><code class="xref py py-class docutils literal"><span class="pre">ImmutableSet</span></code></a>
 
98
derive from <code class="xref py py-class docutils literal"><span class="pre">BaseSet</span></code>, an abstract class useful for determining whether
 
99
something is a set: <code class="docutils literal"><span class="pre">isinstance(obj,</span> <span class="pre">BaseSet)</span></code>.</p>
 
100
<p>The set classes are implemented using dictionaries.  Accordingly, the
 
101
requirements for set elements are the same as those for dictionary keys; namely,
 
102
that the element defines both <a class="reference internal" href="../reference/datamodel.html#object.__eq__" title="object.__eq__"><code class="xref py py-meth docutils literal"><span class="pre">__eq__()</span></code></a> and <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal"><span class="pre">__hash__()</span></code></a>. As a result,
 
103
sets cannot contain mutable elements such as lists or dictionaries. However,
 
104
they can contain immutable collections such as tuples or instances of
 
105
<a class="reference internal" href="#sets.ImmutableSet" title="sets.ImmutableSet"><code class="xref py py-class docutils literal"><span class="pre">ImmutableSet</span></code></a>.  For convenience in implementing sets of sets, inner sets
 
106
are automatically converted to immutable form, for example,
 
107
<code class="docutils literal"><span class="pre">Set([Set(['dog'])])</span></code> is transformed to <code class="docutils literal"><span class="pre">Set([ImmutableSet(['dog'])])</span></code>.</p>
 
108
<dl class="class">
 
109
<dt id="sets.Set">
 
110
<em class="property">class </em><code class="descclassname">sets.</code><code class="descname">Set</code><span class="sig-paren">(</span><span class="optional">[</span><em>iterable</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#sets.Set" title="Permalink to this definition">¶</a></dt>
 
111
<dd><p>Constructs a new empty <a class="reference internal" href="#sets.Set" title="sets.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a> object.  If the optional <em>iterable</em>
 
112
parameter is supplied, updates the set with elements obtained from iteration.
 
113
All of the elements in <em>iterable</em> should be immutable or be transformable to an
 
114
immutable using the protocol described in section <a class="reference internal" href="#immutable-transforms"><span>Protocol for automatic conversion to immutable</span></a>.</p>
 
115
</dd></dl>
 
116
 
 
117
<dl class="class">
 
118
<dt id="sets.ImmutableSet">
 
119
<em class="property">class </em><code class="descclassname">sets.</code><code class="descname">ImmutableSet</code><span class="sig-paren">(</span><span class="optional">[</span><em>iterable</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#sets.ImmutableSet" title="Permalink to this definition">¶</a></dt>
 
120
<dd><p>Constructs a new empty <a class="reference internal" href="#sets.ImmutableSet" title="sets.ImmutableSet"><code class="xref py py-class docutils literal"><span class="pre">ImmutableSet</span></code></a> object.  If the optional <em>iterable</em>
 
121
parameter is supplied, updates the set with elements obtained from iteration.
 
122
All of the elements in <em>iterable</em> should be immutable or be transformable to an
 
123
immutable using the protocol described in section <a class="reference internal" href="#immutable-transforms"><span>Protocol for automatic conversion to immutable</span></a>.</p>
 
124
<p>Because <a class="reference internal" href="#sets.ImmutableSet" title="sets.ImmutableSet"><code class="xref py py-class docutils literal"><span class="pre">ImmutableSet</span></code></a> objects provide a <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal"><span class="pre">__hash__()</span></code></a> method, they
 
125
can be used as set elements or as dictionary keys.  <a class="reference internal" href="#sets.ImmutableSet" title="sets.ImmutableSet"><code class="xref py py-class docutils literal"><span class="pre">ImmutableSet</span></code></a>
 
126
objects do not have methods for adding or removing elements, so all of the
 
127
elements must be known when the constructor is called.</p>
 
128
</dd></dl>
 
129
 
 
130
<div class="section" id="set-objects">
 
131
<span id="id1"></span><h2>8.7.1. Set Objects<a class="headerlink" href="#set-objects" title="Permalink to this headline">¶</a></h2>
 
132
<p>Instances of <a class="reference internal" href="#sets.Set" title="sets.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a> and <a class="reference internal" href="#sets.ImmutableSet" title="sets.ImmutableSet"><code class="xref py py-class docutils literal"><span class="pre">ImmutableSet</span></code></a> both provide the following
 
133
operations:</p>
 
134
<table border="1" class="docutils">
 
135
<colgroup>
 
136
<col width="41%" />
 
137
<col width="16%" />
 
138
<col width="43%" />
 
139
</colgroup>
 
140
<thead valign="bottom">
 
141
<tr class="row-odd"><th class="head">Operation</th>
 
142
<th class="head">Equivalent</th>
 
143
<th class="head">Result</th>
 
144
</tr>
 
145
</thead>
 
146
<tbody valign="top">
 
147
<tr class="row-even"><td><code class="docutils literal"><span class="pre">len(s)</span></code></td>
 
148
<td>&nbsp;</td>
 
149
<td>number of elements in set <em>s</em>
 
150
(cardinality)</td>
 
151
</tr>
 
152
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">s</span></code></td>
 
153
<td>&nbsp;</td>
 
154
<td>test <em>x</em> for membership in <em>s</em></td>
 
155
</tr>
 
156
<tr class="row-even"><td><code class="docutils literal"><span class="pre">x</span> <span class="pre">not</span> <span class="pre">in</span> <span class="pre">s</span></code></td>
 
157
<td>&nbsp;</td>
 
158
<td>test <em>x</em> for non-membership in
 
159
<em>s</em></td>
 
160
</tr>
 
161
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">s.issubset(t)</span></code></td>
 
162
<td><code class="docutils literal"><span class="pre">s</span> <span class="pre">&lt;=</span> <span class="pre">t</span></code></td>
 
163
<td>test whether every element in
 
164
<em>s</em> is in <em>t</em></td>
 
165
</tr>
 
166
<tr class="row-even"><td><code class="docutils literal"><span class="pre">s.issuperset(t)</span></code></td>
 
167
<td><code class="docutils literal"><span class="pre">s</span> <span class="pre">&gt;=</span> <span class="pre">t</span></code></td>
 
168
<td>test whether every element in
 
169
<em>t</em> is in <em>s</em></td>
 
170
</tr>
 
171
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">s.union(t)</span></code></td>
 
172
<td><code class="docutils literal"><span class="pre">s</span> <span class="pre">|</span> <span class="pre">t</span></code></td>
 
173
<td>new set with elements from both
 
174
<em>s</em> and <em>t</em></td>
 
175
</tr>
 
176
<tr class="row-even"><td><code class="docutils literal"><span class="pre">s.intersection(t)</span></code></td>
 
177
<td><code class="docutils literal"><span class="pre">s</span> <span class="pre">&amp;</span> <span class="pre">t</span></code></td>
 
178
<td>new set with elements common to
 
179
<em>s</em> and <em>t</em></td>
 
180
</tr>
 
181
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">s.difference(t)</span></code></td>
 
182
<td><code class="docutils literal"><span class="pre">s</span> <span class="pre">-</span> <span class="pre">t</span></code></td>
 
183
<td>new set with elements in <em>s</em>
 
184
but not in <em>t</em></td>
 
185
</tr>
 
186
<tr class="row-even"><td><code class="docutils literal"><span class="pre">s.symmetric_difference(t)</span></code></td>
 
187
<td><code class="docutils literal"><span class="pre">s</span> <span class="pre">^</span> <span class="pre">t</span></code></td>
 
188
<td>new set with elements in either
 
189
<em>s</em> or <em>t</em> but not both</td>
 
190
</tr>
 
191
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">s.copy()</span></code></td>
 
192
<td>&nbsp;</td>
 
193
<td>new set with a shallow copy of
 
194
<em>s</em></td>
 
195
</tr>
 
196
</tbody>
 
197
</table>
 
198
<p>Note, the non-operator versions of <code class="xref py py-meth docutils literal"><span class="pre">union()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">intersection()</span></code>,
 
199
<code class="xref py py-meth docutils literal"><span class="pre">difference()</span></code>, and <code class="xref py py-meth docutils literal"><span class="pre">symmetric_difference()</span></code> will accept any iterable as
 
200
an argument. In contrast, their operator based counterparts require their
 
201
arguments to be sets.  This precludes error-prone constructions like
 
202
<code class="docutils literal"><span class="pre">Set('abc')</span> <span class="pre">&amp;</span> <span class="pre">'cbs'</span></code> in favor of the more readable
 
203
<code class="docutils literal"><span class="pre">Set('abc').intersection('cbs')</span></code>.</p>
 
204
<div class="versionchanged">
 
205
<p><span class="versionmodified">Changed in version 2.3.1: </span>Formerly all arguments were required to be sets.</p>
 
206
</div>
 
207
<p>In addition, both <a class="reference internal" href="#sets.Set" title="sets.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a> and <a class="reference internal" href="#sets.ImmutableSet" title="sets.ImmutableSet"><code class="xref py py-class docutils literal"><span class="pre">ImmutableSet</span></code></a> support set to set
 
208
comparisons.  Two sets are equal if and only if every element of each set is
 
209
contained in the other (each is a subset of the other). A set is less than
 
210
another set if and only if the first set is a proper subset of the second set
 
211
(is a subset, but is not equal). A set is greater than another set if and only
 
212
if the first set is a proper superset of the second set (is a superset, but is
 
213
not equal).</p>
 
214
<p>The subset and equality comparisons do not generalize to a complete ordering
 
215
function.  For example, any two disjoint sets are not equal and are not subsets
 
216
of each other, so <em>all</em> of the following return <code class="docutils literal"><span class="pre">False</span></code>:  <code class="docutils literal"><span class="pre">a&lt;b</span></code>, <code class="docutils literal"><span class="pre">a==b</span></code>,
 
217
or <code class="docutils literal"><span class="pre">a&gt;b</span></code>. Accordingly, sets do not implement the <a class="reference internal" href="../reference/datamodel.html#object.__cmp__" title="object.__cmp__"><code class="xref py py-meth docutils literal"><span class="pre">__cmp__()</span></code></a> method.</p>
 
218
<p>Since sets only define partial ordering (subset relationships), the output of
 
219
the <code class="xref py py-meth docutils literal"><span class="pre">list.sort()</span></code> method is undefined for lists of sets.</p>
 
220
<p>The following table lists operations available in <a class="reference internal" href="#sets.ImmutableSet" title="sets.ImmutableSet"><code class="xref py py-class docutils literal"><span class="pre">ImmutableSet</span></code></a> but not
 
221
found in <a class="reference internal" href="#sets.Set" title="sets.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a>:</p>
 
222
<table border="1" class="docutils">
 
223
<colgroup>
 
224
<col width="30%" />
 
225
<col width="70%" />
 
226
</colgroup>
 
227
<thead valign="bottom">
 
228
<tr class="row-odd"><th class="head">Operation</th>
 
229
<th class="head">Result</th>
 
230
</tr>
 
231
</thead>
 
232
<tbody valign="top">
 
233
<tr class="row-even"><td><code class="docutils literal"><span class="pre">hash(s)</span></code></td>
 
234
<td>returns a hash value for <em>s</em></td>
 
235
</tr>
 
236
</tbody>
 
237
</table>
 
238
<p>The following table lists operations available in <a class="reference internal" href="#sets.Set" title="sets.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a> but not found in
 
239
<a class="reference internal" href="#sets.ImmutableSet" title="sets.ImmutableSet"><code class="xref py py-class docutils literal"><span class="pre">ImmutableSet</span></code></a>:</p>
 
240
<table border="1" class="docutils">
 
241
<colgroup>
 
242
<col width="45%" />
 
243
<col width="15%" />
 
244
<col width="39%" />
 
245
</colgroup>
 
246
<thead valign="bottom">
 
247
<tr class="row-odd"><th class="head">Operation</th>
 
248
<th class="head">Equivalent</th>
 
249
<th class="head">Result</th>
 
250
</tr>
 
251
</thead>
 
252
<tbody valign="top">
 
253
<tr class="row-even"><td><code class="docutils literal"><span class="pre">s.update(t)</span></code></td>
 
254
<td><em>s</em> |= <em>t</em></td>
 
255
<td>return set <em>s</em> with elements
 
256
added from <em>t</em></td>
 
257
</tr>
 
258
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">s.intersection_update(t)</span></code></td>
 
259
<td><em>s</em> &amp;= <em>t</em></td>
 
260
<td>return set <em>s</em> keeping only
 
261
elements also found in <em>t</em></td>
 
262
</tr>
 
263
<tr class="row-even"><td><code class="docutils literal"><span class="pre">s.difference_update(t)</span></code></td>
 
264
<td><em>s</em> -= <em>t</em></td>
 
265
<td>return set <em>s</em> after removing
 
266
elements found in <em>t</em></td>
 
267
</tr>
 
268
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">s.symmetric_difference_update(t)</span></code></td>
 
269
<td><em>s</em> ^= <em>t</em></td>
 
270
<td>return set <em>s</em> with elements
 
271
from <em>s</em> or <em>t</em> but not both</td>
 
272
</tr>
 
273
<tr class="row-even"><td><code class="docutils literal"><span class="pre">s.add(x)</span></code></td>
 
274
<td>&nbsp;</td>
 
275
<td>add element <em>x</em> to set <em>s</em></td>
 
276
</tr>
 
277
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">s.remove(x)</span></code></td>
 
278
<td>&nbsp;</td>
 
279
<td>remove <em>x</em> from set <em>s</em>; raises
 
280
<a class="reference internal" href="exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><code class="xref py py-exc docutils literal"><span class="pre">KeyError</span></code></a> if not present</td>
 
281
</tr>
 
282
<tr class="row-even"><td><code class="docutils literal"><span class="pre">s.discard(x)</span></code></td>
 
283
<td>&nbsp;</td>
 
284
<td>removes <em>x</em> from set <em>s</em> if
 
285
present</td>
 
286
</tr>
 
287
<tr class="row-odd"><td><code class="docutils literal"><span class="pre">s.pop()</span></code></td>
 
288
<td>&nbsp;</td>
 
289
<td>remove and return an arbitrary
 
290
element from <em>s</em>; raises
 
291
<a class="reference internal" href="exceptions.html#exceptions.KeyError" title="exceptions.KeyError"><code class="xref py py-exc docutils literal"><span class="pre">KeyError</span></code></a> if empty</td>
 
292
</tr>
 
293
<tr class="row-even"><td><code class="docutils literal"><span class="pre">s.clear()</span></code></td>
 
294
<td>&nbsp;</td>
 
295
<td>remove all elements from set
 
296
<em>s</em></td>
 
297
</tr>
 
298
</tbody>
 
299
</table>
 
300
<p>Note, the non-operator versions of <code class="xref py py-meth docutils literal"><span class="pre">update()</span></code>, <code class="xref py py-meth docutils literal"><span class="pre">intersection_update()</span></code>,
 
301
<code class="xref py py-meth docutils literal"><span class="pre">difference_update()</span></code>, and <code class="xref py py-meth docutils literal"><span class="pre">symmetric_difference_update()</span></code> will accept
 
302
any iterable as an argument.</p>
 
303
<div class="versionchanged">
 
304
<p><span class="versionmodified">Changed in version 2.3.1: </span>Formerly all arguments were required to be sets.</p>
 
305
</div>
 
306
<p>Also note, the module also includes a <code class="xref py py-meth docutils literal"><span class="pre">union_update()</span></code> method which is an
 
307
alias for <code class="xref py py-meth docutils literal"><span class="pre">update()</span></code>.  The method is included for backwards compatibility.
 
308
Programmers should prefer the <code class="xref py py-meth docutils literal"><span class="pre">update()</span></code> method because it is supported by
 
309
the built-in <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-class docutils literal"><span class="pre">set()</span></code></a> and <a class="reference internal" href="stdtypes.html#frozenset" title="frozenset"><code class="xref py py-class docutils literal"><span class="pre">frozenset()</span></code></a> types.</p>
 
310
</div>
 
311
<div class="section" id="example">
 
312
<span id="set-example"></span><h2>8.7.2. Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2>
 
313
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sets</span> <span class="kn">import</span> <span class="n">Set</span>
 
314
<span class="gp">&gt;&gt;&gt; </span><span class="n">engineers</span> <span class="o">=</span> <span class="n">Set</span><span class="p">([</span><span class="s1">&#39;John&#39;</span><span class="p">,</span> <span class="s1">&#39;Jane&#39;</span><span class="p">,</span> <span class="s1">&#39;Jack&#39;</span><span class="p">,</span> <span class="s1">&#39;Janice&#39;</span><span class="p">])</span>
 
315
<span class="gp">&gt;&gt;&gt; </span><span class="n">programmers</span> <span class="o">=</span> <span class="n">Set</span><span class="p">([</span><span class="s1">&#39;Jack&#39;</span><span class="p">,</span> <span class="s1">&#39;Sam&#39;</span><span class="p">,</span> <span class="s1">&#39;Susan&#39;</span><span class="p">,</span> <span class="s1">&#39;Janice&#39;</span><span class="p">])</span>
 
316
<span class="gp">&gt;&gt;&gt; </span><span class="n">managers</span> <span class="o">=</span> <span class="n">Set</span><span class="p">([</span><span class="s1">&#39;Jane&#39;</span><span class="p">,</span> <span class="s1">&#39;Jack&#39;</span><span class="p">,</span> <span class="s1">&#39;Susan&#39;</span><span class="p">,</span> <span class="s1">&#39;Zack&#39;</span><span class="p">])</span>
 
317
<span class="gp">&gt;&gt;&gt; </span><span class="n">employees</span> <span class="o">=</span> <span class="n">engineers</span> <span class="o">|</span> <span class="n">programmers</span> <span class="o">|</span> <span class="n">managers</span>           <span class="c1"># union</span>
 
318
<span class="gp">&gt;&gt;&gt; </span><span class="n">engineering_management</span> <span class="o">=</span> <span class="n">engineers</span> <span class="o">&amp;</span> <span class="n">managers</span>            <span class="c1"># intersection</span>
 
319
<span class="gp">&gt;&gt;&gt; </span><span class="n">fulltime_management</span> <span class="o">=</span> <span class="n">managers</span> <span class="o">-</span> <span class="n">engineers</span> <span class="o">-</span> <span class="n">programmers</span> <span class="c1"># difference</span>
 
320
<span class="gp">&gt;&gt;&gt; </span><span class="n">engineers</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="s1">&#39;Marvin&#39;</span><span class="p">)</span>                                  <span class="c1"># add element</span>
 
321
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">engineers</span> 
 
322
<span class="go">Set([&#39;Jane&#39;, &#39;Marvin&#39;, &#39;Janice&#39;, &#39;John&#39;, &#39;Jack&#39;])</span>
 
323
<span class="gp">&gt;&gt;&gt; </span><span class="n">employees</span><span class="o">.</span><span class="n">issuperset</span><span class="p">(</span><span class="n">engineers</span><span class="p">)</span>     <span class="c1"># superset test</span>
 
324
<span class="go">False</span>
 
325
<span class="gp">&gt;&gt;&gt; </span><span class="n">employees</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">engineers</span><span class="p">)</span>         <span class="c1"># update from another set</span>
 
326
<span class="gp">&gt;&gt;&gt; </span><span class="n">employees</span><span class="o">.</span><span class="n">issuperset</span><span class="p">(</span><span class="n">engineers</span><span class="p">)</span>
 
327
<span class="go">True</span>
 
328
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">group</span> <span class="ow">in</span> <span class="p">[</span><span class="n">engineers</span><span class="p">,</span> <span class="n">programmers</span><span class="p">,</span> <span class="n">managers</span><span class="p">,</span> <span class="n">employees</span><span class="p">]:</span> 
 
329
<span class="gp">... </span>    <span class="n">group</span><span class="o">.</span><span class="n">discard</span><span class="p">(</span><span class="s1">&#39;Susan&#39;</span><span class="p">)</span>          <span class="c1"># unconditionally remove element</span>
 
330
<span class="gp">... </span>    <span class="k">print</span> <span class="n">group</span>
 
331
<span class="gp">...</span>
 
332
<span class="go">Set([&#39;Jane&#39;, &#39;Marvin&#39;, &#39;Janice&#39;, &#39;John&#39;, &#39;Jack&#39;])</span>
 
333
<span class="go">Set([&#39;Janice&#39;, &#39;Jack&#39;, &#39;Sam&#39;])</span>
 
334
<span class="go">Set([&#39;Jane&#39;, &#39;Zack&#39;, &#39;Jack&#39;])</span>
 
335
<span class="go">Set([&#39;Jack&#39;, &#39;Sam&#39;, &#39;Jane&#39;, &#39;Marvin&#39;, &#39;Janice&#39;, &#39;John&#39;, &#39;Zack&#39;])</span>
 
336
</pre></div>
 
337
</div>
 
338
</div>
 
339
<div class="section" id="protocol-for-automatic-conversion-to-immutable">
 
340
<span id="immutable-transforms"></span><h2>8.7.3. Protocol for automatic conversion to immutable<a class="headerlink" href="#protocol-for-automatic-conversion-to-immutable" title="Permalink to this headline">¶</a></h2>
 
341
<p>Sets can only contain immutable elements.  For convenience, mutable <a class="reference internal" href="#sets.Set" title="sets.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a>
 
342
objects are automatically copied to an <a class="reference internal" href="#sets.ImmutableSet" title="sets.ImmutableSet"><code class="xref py py-class docutils literal"><span class="pre">ImmutableSet</span></code></a> before being added
 
343
as a set element.</p>
 
344
<p>The mechanism is to always add a <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a> element, or if it is not
 
345
hashable, the element is checked to see if it has an <code class="xref py py-meth docutils literal"><span class="pre">__as_immutable__()</span></code>
 
346
method which returns an immutable equivalent.</p>
 
347
<p>Since <a class="reference internal" href="#sets.Set" title="sets.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a> objects have a <code class="xref py py-meth docutils literal"><span class="pre">__as_immutable__()</span></code> method returning an
 
348
instance of <a class="reference internal" href="#sets.ImmutableSet" title="sets.ImmutableSet"><code class="xref py py-class docutils literal"><span class="pre">ImmutableSet</span></code></a>, it is possible to construct sets of sets.</p>
 
349
<p>A similar mechanism is needed by the <a class="reference internal" href="../reference/datamodel.html#object.__contains__" title="object.__contains__"><code class="xref py py-meth docutils literal"><span class="pre">__contains__()</span></code></a> and <code class="xref py py-meth docutils literal"><span class="pre">remove()</span></code>
 
350
methods which need to hash an element to check for membership in a set.  Those
 
351
methods check an element for hashability and, if not, check for a
 
352
<code class="xref py py-meth docutils literal"><span class="pre">__as_temporarily_immutable__()</span></code> method which returns the element wrapped by
 
353
a class that provides temporary methods for <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><code class="xref py py-meth docutils literal"><span class="pre">__hash__()</span></code></a>, <a class="reference internal" href="../reference/datamodel.html#object.__eq__" title="object.__eq__"><code class="xref py py-meth docutils literal"><span class="pre">__eq__()</span></code></a>,
 
354
and <a class="reference internal" href="../reference/datamodel.html#object.__ne__" title="object.__ne__"><code class="xref py py-meth docutils literal"><span class="pre">__ne__()</span></code></a>.</p>
 
355
<p>The alternate mechanism spares the need to build a separate copy of the original
 
356
mutable object.</p>
 
357
<p><a class="reference internal" href="#sets.Set" title="sets.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a> objects implement the <code class="xref py py-meth docutils literal"><span class="pre">__as_temporarily_immutable__()</span></code> method
 
358
which returns the <a class="reference internal" href="#sets.Set" title="sets.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a> object wrapped by a new class
 
359
<code class="xref py py-class docutils literal"><span class="pre">_TemporarilyImmutableSet</span></code>.</p>
 
360
<p>The two mechanisms for adding hashability are normally invisible to the user;
 
361
however, a conflict can arise in a multi-threaded environment where one thread
 
362
is updating a set while another has temporarily wrapped it in
 
363
<code class="xref py py-class docutils literal"><span class="pre">_TemporarilyImmutableSet</span></code>.  In other words, sets of mutable sets are not
 
364
thread-safe.</p>
 
365
</div>
 
366
<div class="section" id="comparison-to-the-built-in-set-types">
 
367
<span id="comparison-to-builtin-set"></span><h2>8.7.4. Comparison to the built-in <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-class docutils literal"><span class="pre">set</span></code></a> types<a class="headerlink" href="#comparison-to-the-built-in-set-types" title="Permalink to this headline">¶</a></h2>
 
368
<p>The built-in <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-class docutils literal"><span class="pre">set</span></code></a> and <a class="reference internal" href="stdtypes.html#frozenset" title="frozenset"><code class="xref py py-class docutils literal"><span class="pre">frozenset</span></code></a> types were designed based on
 
369
lessons learned from the <a class="reference internal" href="#module-sets" title="sets: Implementation of sets of unique elements. (deprecated)"><code class="xref py py-mod docutils literal"><span class="pre">sets</span></code></a> module.  The key differences are:</p>
 
370
<ul class="simple">
 
371
<li><a class="reference internal" href="#sets.Set" title="sets.Set"><code class="xref py py-class docutils literal"><span class="pre">Set</span></code></a> and <a class="reference internal" href="#sets.ImmutableSet" title="sets.ImmutableSet"><code class="xref py py-class docutils literal"><span class="pre">ImmutableSet</span></code></a> were renamed to <a class="reference internal" href="stdtypes.html#set" title="set"><code class="xref py py-class docutils literal"><span class="pre">set</span></code></a> and
 
372
<a class="reference internal" href="stdtypes.html#frozenset" title="frozenset"><code class="xref py py-class docutils literal"><span class="pre">frozenset</span></code></a>.</li>
 
373
<li>There is no equivalent to <code class="xref py py-class docutils literal"><span class="pre">BaseSet</span></code>.  Instead, use <code class="docutils literal"><span class="pre">isinstance(x,</span>
 
374
<span class="pre">(set,</span> <span class="pre">frozenset))</span></code>.</li>
 
375
<li>The hash algorithm for the built-ins performs significantly better (fewer
 
376
collisions) for most datasets.</li>
 
377
<li>The built-in versions have more space efficient pickles.</li>
 
378
<li>The built-in versions do not have a <code class="xref py py-meth docutils literal"><span class="pre">union_update()</span></code> method. Instead, use
 
379
the <code class="xref py py-meth docutils literal"><span class="pre">update()</span></code> method which is equivalent.</li>
 
380
<li>The built-in versions do not have a <code class="docutils literal"><span class="pre">_repr(sorted=True)</span></code> method.
 
381
Instead, use the built-in <a class="reference internal" href="functions.html#repr" title="repr"><code class="xref py py-func docutils literal"><span class="pre">repr()</span></code></a> and <a class="reference internal" href="functions.html#sorted" title="sorted"><code class="xref py py-func docutils literal"><span class="pre">sorted()</span></code></a> functions:
 
382
<code class="docutils literal"><span class="pre">repr(sorted(s))</span></code>.</li>
 
383
<li>The built-in version does not have a protocol for automatic conversion to
 
384
immutable.  Many found this feature to be confusing and no one in the community
 
385
reported having found real uses for it.</li>
 
386
</ul>
 
387
</div>
 
388
</div>
 
389
 
 
390
 
 
391
          </div>
 
392
        </div>
 
393
      </div>
 
394
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
 
395
        <div class="sphinxsidebarwrapper">
 
396
  <h3><a href="../contents.html">Table Of Contents</a></h3>
 
397
  <ul>
 
398
<li><a class="reference internal" href="#">8.7. <code class="docutils literal"><span class="pre">sets</span></code> &#8212; Unordered collections of unique elements</a><ul>
 
399
<li><a class="reference internal" href="#set-objects">8.7.1. Set Objects</a></li>
 
400
<li><a class="reference internal" href="#example">8.7.2. Example</a></li>
 
401
<li><a class="reference internal" href="#protocol-for-automatic-conversion-to-immutable">8.7.3. Protocol for automatic conversion to immutable</a></li>
 
402
<li><a class="reference internal" href="#comparison-to-the-built-in-set-types">8.7.4. Comparison to the built-in <code class="docutils literal"><span class="pre">set</span></code> types</a></li>
 
403
</ul>
 
404
</li>
 
405
</ul>
 
406
 
 
407
  <h4>Previous topic</h4>
 
408
  <p class="topless"><a href="array.html"
 
409
                        title="previous chapter">8.6. <code class="docutils literal"><span class="pre">array</span></code> &#8212; Efficient arrays of numeric values</a></p>
 
410
  <h4>Next topic</h4>
 
411
  <p class="topless"><a href="sched.html"
 
412
                        title="next chapter">8.8. <code class="docutils literal"><span class="pre">sched</span></code> &#8212; Event scheduler</a></p>
 
413
<h3>This Page</h3>
 
414
<ul class="this-page-menu">
 
415
  <li><a href="../bugs.html">Report a Bug</a></li>
 
416
  <li><a href="../_sources/library/sets.txt"
 
417
         rel="nofollow">Show Source</a></li>
 
418
</ul>
 
419
 
 
420
<div id="searchbox" style="display: none" role="search">
 
421
  <h3>Quick search</h3>
 
422
    <form class="search" action="../search.html" method="get">
 
423
      <input type="text" name="q" />
 
424
      <input type="submit" value="Go" />
 
425
      <input type="hidden" name="check_keywords" value="yes" />
 
426
      <input type="hidden" name="area" value="default" />
 
427
    </form>
 
428
    <p class="searchtip" style="font-size: 90%">
 
429
    Enter search terms or a module, class or function name.
 
430
    </p>
 
431
</div>
 
432
<script type="text/javascript">$('#searchbox').show(0);</script>
 
433
        </div>
 
434
      </div>
 
435
      <div class="clearer"></div>
 
436
    </div>  
 
437
    <div class="related" role="navigation" aria-label="related navigation">
 
438
      <h3>Navigation</h3>
 
439
      <ul>
 
440
        <li class="right" style="margin-right: 10px">
 
441
          <a href="../genindex.html" title="General Index"
 
442
             >index</a></li>
 
443
        <li class="right" >
 
444
          <a href="../py-modindex.html" title="Python Module Index"
 
445
             >modules</a> |</li>
 
446
        <li class="right" >
 
447
          <a href="sched.html" title="8.8. sched — Event scheduler"
 
448
             >next</a> |</li>
 
449
        <li class="right" >
 
450
          <a href="array.html" title="8.6. array — Efficient arrays of numeric values"
 
451
             >previous</a> |</li>
 
452
        <li><img src="../_static/py.png" alt=""
 
453
                 style="vertical-align: middle; margin-top: -1px"/></li>
 
454
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
 
455
        <li>
 
456
          <span class="version_switcher_placeholder">2.7.12</span>
 
457
          <a href="../index.html">Documentation</a> &raquo;
 
458
        </li>
 
459
 
 
460
          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &raquo;</li>
 
461
          <li class="nav-item nav-item-2"><a href="datatypes.html" >8. Data Types</a> &raquo;</li> 
 
462
      </ul>
 
463
    </div>  
 
464
    <div class="footer">
 
465
    &copy; <a href="../copyright.html">Copyright</a> 1990-2016, Python Software Foundation.
 
466
    <br />
 
467
    The Python Software Foundation is a non-profit corporation.
 
468
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
 
469
    <br />
 
470
    Last updated on Sep 20, 2016.
 
471
    <a href="../bugs.html">Found a bug</a>?
 
472
    <br />
 
473
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.3.3.
 
474
    </div>
 
475
 
 
476
  </body>
 
477
</html>
 
 
b'\\ No newline at end of file'