| 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="set-example.html" /> |
| 13 | <link rel="prev" href="module-sets.html" /> |
| 14 | <link rel="parent" href="module-sets.html" /> |
| 15 | <link rel="next" href="set-example.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>5.15.1 Set Objects </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.15 sets " |
| 25 | href="module-sets.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.15 sets " |
| 28 | href="module-sets.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.2 Example" |
| 31 | href="set-example.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-sets.html">5.15 sets </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-sets.html">5.15 sets </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="set-example.html">5.15.2 Example</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION0071510000000000000000"></A><A NAME="set-objects"></A> |
| 56 | <BR> |
| 57 | 5.15.1 Set Objects |
| 58 | </H2> |
| 59 | |
| 60 | <P> |
| 61 | Instances of <tt class="class">Set</tt> and <tt class="class">ImmutableSet</tt> both provide |
| 62 | the following operations: |
| 63 | |
| 64 | <P> |
| 65 | <div class="center"><table class="realtable"> |
| 66 | <thead> |
| 67 | <tr> |
| 68 | <th class="center">Operation</th> |
| 69 | <th class="center">Equivalent</th> |
| 70 | <th class="left" >Result</th> |
| 71 | </tr> |
| 72 | </thead> |
| 73 | <tbody> |
| 74 | <tr><td class="center" valign="baseline"><code>len(<var>s</var>)</code></td> |
| 75 | <td class="center"></td> |
| 76 | <td class="left" >cardinality of set <var>s</var></td></tr><P> |
| 77 | |
| 78 | <tr><td class="center" valign="baseline"><code><var>x</var> in <var>s</var></code></td> |
| 79 | <td class="center"></td> |
| 80 | <td class="left" >test <var>x</var> for membership in <var>s</var></td></tr> |
| 81 | <tr><td class="center" valign="baseline"><code><var>x</var> not in <var>s</var></code></td> |
| 82 | <td class="center"></td> |
| 83 | <td class="left" >test <var>x</var> for non-membership in <var>s</var></td></tr> |
| 84 | <tr><td class="center" valign="baseline"><code><var>s</var>.issubset(<var>t</var>)</code></td> |
| 85 | <td class="center"><code><var>s</var> <= <var>t</var></code></td> |
| 86 | <td class="left" >test whether every element in <var>s</var> is in <var>t</var></td></tr> |
| 87 | <tr><td class="center" valign="baseline"><code><var>s</var>.issuperset(<var>t</var>)</code></td> |
| 88 | <td class="center"><code><var>s</var> >= <var>t</var></code></td> |
| 89 | <td class="left" >test whether every element in <var>t</var> is in <var>s</var></td></tr><P> |
| 90 | |
| 91 | <tr><td class="center" valign="baseline"><code><var>s</var>.union(<var>t</var>)</code></td> |
| 92 | <td class="center"><var>s</var> | <var>t</var></td> |
| 93 | <td class="left" >new set with elements from both <var>s</var> and <var>t</var></td></tr> |
| 94 | <tr><td class="center" valign="baseline"><code><var>s</var>.intersection(<var>t</var>)</code></td> |
| 95 | <td class="center"><var>s</var> & <var>t</var></td> |
| 96 | <td class="left" >new set with elements common to <var>s</var> and <var>t</var></td></tr> |
| 97 | <tr><td class="center" valign="baseline"><code><var>s</var>.difference(<var>t</var>)</code></td> |
| 98 | <td class="center"><var>s</var> - <var>t</var></td> |
| 99 | <td class="left" >new set with elements in <var>s</var> but not in <var>t</var></td></tr> |
| 100 | <tr><td class="center" valign="baseline"><code><var>s</var>.symmetric_difference(<var>t</var>)</code></td> |
| 101 | <td class="center"><var>s</var> ^ <var>t</var></td> |
| 102 | <td class="left" >new set with elements in either <var>s</var> or <var>t</var> but not both</td></tr> |
| 103 | <tr><td class="center" valign="baseline"><code><var>s</var>.copy()</code></td> |
| 104 | <td class="center"></td> |
| 105 | <td class="left" >new set with a shallow copy of <var>s</var></td></tr></tbody> |
| 106 | </table></div> |
| 107 | |
| 108 | <P> |
| 109 | Note, the non-operator versions of <tt class="method">union()</tt>, |
| 110 | <tt class="method">intersection()</tt>, <tt class="method">difference()</tt>, and |
| 111 | <tt class="method">symmetric_difference()</tt> will accept any iterable as an argument. |
| 112 | In contrast, their operator based counterparts require their arguments to |
| 113 | be sets. This precludes error-prone constructions like |
| 114 | <code>Set('abc') & 'cbs'</code> in favor of the more readable |
| 115 | <code>Set('abc').intersection('cbs')</code>. |
| 116 | |
| 117 | <span class="versionnote">Changed in version 2.3.1: |
| 118 | Formerly all arguments were required to be sets.</span> |
| 119 | |
| 120 | <P> |
| 121 | In addition, both <tt class="class">Set</tt> and <tt class="class">ImmutableSet</tt> |
| 122 | support set to set comparisons. Two sets are equal if and only if |
| 123 | every element of each set is contained in the other (each is a subset |
| 124 | of the other). |
| 125 | A set is less than another set if and only if the first set is a proper |
| 126 | subset of the second set (is a subset, but is not equal). |
| 127 | A set is greater than another set if and only if the first set is a proper |
| 128 | superset of the second set (is a superset, but is not equal). |
| 129 | |
| 130 | <P> |
| 131 | The subset and equality comparisons do not generalize to a complete |
| 132 | ordering function. For example, any two disjoint sets are not equal and |
| 133 | are not subsets of each other, so <em>all</em> of the following return |
| 134 | <code>False</code>: <code><var>a</var><<var>b</var></code>, <code><var>a</var>==<var>b</var></code>, or |
| 135 | <code><var>a</var>><var>b</var></code>. |
| 136 | Accordingly, sets do not implement the <tt class="method">__cmp__</tt> method. |
| 137 | |
| 138 | <P> |
| 139 | Since sets only define partial ordering (subset relationships), the output |
| 140 | of the <tt class="method">list.sort()</tt> method is undefined for lists of sets. |
| 141 | |
| 142 | <P> |
| 143 | The following table lists operations available in <tt class="class">ImmutableSet</tt> |
| 144 | but not found in <tt class="class">Set</tt>: |
| 145 | |
| 146 | <P> |
| 147 | <div class="center"><table class="realtable"> |
| 148 | <thead> |
| 149 | <tr> |
| 150 | <th class="center">Operation</th> |
| 151 | <th class="left" >Result</th> |
| 152 | </tr> |
| 153 | </thead> |
| 154 | <tbody> |
| 155 | <tr><td class="center" valign="baseline"><code>hash(<var>s</var>)</code></td> |
| 156 | <td class="left" >returns a hash value for <var>s</var></td></tr></tbody> |
| 157 | </table></div> |
| 158 | |
| 159 | <P> |
| 160 | The following table lists operations available in <tt class="class">Set</tt> |
| 161 | but not found in <tt class="class">ImmutableSet</tt>: |
| 162 | |
| 163 | <P> |
| 164 | <div class="center"><table class="realtable"> |
| 165 | <thead> |
| 166 | <tr> |
| 167 | <th class="center">Operation</th> |
| 168 | <th class="center">Equivalent</th> |
| 169 | <th class="left" >Result</th> |
| 170 | </tr> |
| 171 | </thead> |
| 172 | <tbody> |
| 173 | <tr><td class="center" valign="baseline"><code><var>s</var>.update(<var>t</var>)</code></td> |
| 174 | <td class="center"><var>s</var> |= <var>t</var></td> |
| 175 | <td class="left" >return set <var>s</var> with elements added from <var>t</var></td></tr> |
| 176 | <tr><td class="center" valign="baseline"><code><var>s</var>.intersection_update(<var>t</var>)</code></td> |
| 177 | <td class="center"><var>s</var> &= <var>t</var></td> |
| 178 | <td class="left" >return set <var>s</var> keeping only elements also found in <var>t</var></td></tr> |
| 179 | <tr><td class="center" valign="baseline"><code><var>s</var>.difference_update(<var>t</var>)</code></td> |
| 180 | <td class="center"><var>s</var> -= <var>t</var></td> |
| 181 | <td class="left" >return set <var>s</var> after removing elements found in <var>t</var></td></tr> |
| 182 | <tr><td class="center" valign="baseline"><code><var>s</var>.symmetric_difference_update(<var>t</var>)</code></td> |
| 183 | <td class="center"><var>s</var> ^= <var>t</var></td> |
| 184 | <td class="left" >return set <var>s</var> with elements from <var>s</var> or <var>t</var> |
| 185 | but not both</td></tr><P> |
| 186 | |
| 187 | <tr><td class="center" valign="baseline"><code><var>s</var>.add(<var>x</var>)</code></td> |
| 188 | <td class="center"></td> |
| 189 | <td class="left" >add element <var>x</var> to set <var>s</var></td></tr> |
| 190 | <tr><td class="center" valign="baseline"><code><var>s</var>.remove(<var>x</var>)</code></td> |
| 191 | <td class="center"></td> |
| 192 | <td class="left" >remove <var>x</var> from set <var>s</var>; raises KeyError if not present</td></tr> |
| 193 | <tr><td class="center" valign="baseline"><code><var>s</var>.discard(<var>x</var>)</code></td> |
| 194 | <td class="center"></td> |
| 195 | <td class="left" >removes <var>x</var> from set <var>s</var> if present</td></tr> |
| 196 | <tr><td class="center" valign="baseline"><code><var>s</var>.pop()</code></td> |
| 197 | <td class="center"></td> |
| 198 | <td class="left" >remove and return an arbitrary element from <var>s</var>; raises |
| 199 | KeyError if empty</td></tr> |
| 200 | <tr><td class="center" valign="baseline"><code><var>s</var>.clear()</code></td> |
| 201 | <td class="center"></td> |
| 202 | <td class="left" >remove all elements from set <var>s</var></td></tr></tbody> |
| 203 | </table></div> |
| 204 | |
| 205 | <P> |
| 206 | Note, the non-operator versions of <tt class="method">update()</tt>, |
| 207 | <tt class="method">intersection_update()</tt>, <tt class="method">difference_update()</tt>, and |
| 208 | <tt class="method">symmetric_difference_update()</tt> will accept any iterable as |
| 209 | an argument. |
| 210 | |
| 211 | <span class="versionnote">Changed in version 2.3.1: |
| 212 | Formerly all arguments were required to be sets.</span> |
| 213 | |
| 214 | <P> |
| 215 | Also note, the module also includes a <tt class="method">union_update()</tt> method |
| 216 | which is an alias for <tt class="method">update()</tt>. The method is included for |
| 217 | backwards compatibility. Programmers should prefer the |
| 218 | <tt class="method">update()</tt> method because it the one supported by the builtin |
| 219 | <tt class="class">set()</tt> and <tt class="class">frozenset()</tt> types. |
| 220 | |
| 221 | <P> |
| 222 | |
| 223 | <DIV CLASS="navigation"> |
| 224 | <div class='online-navigation'> |
| 225 | <p></p><hr /> |
| 226 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 227 | <tr> |
| 228 | <td class='online-navigation'><a rel="prev" title="5.15 sets " |
| 229 | href="module-sets.html"><img src='../icons/previous.png' |
| 230 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 231 | <td class='online-navigation'><a rel="parent" title="5.15 sets " |
| 232 | href="module-sets.html"><img src='../icons/up.png' |
| 233 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 234 | <td class='online-navigation'><a rel="next" title="5.15.2 Example" |
| 235 | href="set-example.html"><img src='../icons/next.png' |
| 236 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 237 | <td align="center" width="100%">Python Library Reference</td> |
| 238 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 239 | href="contents.html"><img src='../icons/contents.png' |
| 240 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 241 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 242 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 243 | <td class='online-navigation'><a rel="index" title="Index" |
| 244 | href="genindex.html"><img src='../icons/index.png' |
| 245 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 246 | </tr></table> |
| 247 | <div class='online-navigation'> |
| 248 | <b class="navlabel">Previous:</b> |
| 249 | <a class="sectref" rel="prev" href="module-sets.html">5.15 sets </A> |
| 250 | <b class="navlabel">Up:</b> |
| 251 | <a class="sectref" rel="parent" href="module-sets.html">5.15 sets </A> |
| 252 | <b class="navlabel">Next:</b> |
| 253 | <a class="sectref" rel="next" href="set-example.html">5.15.2 Example</A> |
| 254 | </div> |
| 255 | </div> |
| 256 | <hr /> |
| 257 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 258 | </DIV> |
| 259 | <!--End of Navigation Panel--> |
| 260 | <ADDRESS> |
| 261 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 262 | </ADDRESS> |
| 263 | </BODY> |
| 264 | </HTML> |