Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="whatsnew24.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="whatsnew24.html" title='What's New in Python 2.4' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='last' href='about.html' title='About this document...' /> | |
10 | <link rel='help' href='about.html' title='About this document...' /> | |
11 | <link rel="next" href="node3.html" /> | |
12 | <link rel="prev" href="contents.html" /> | |
13 | <link rel="parent" href="whatsnew24.html" /> | |
14 | <link rel="next" href="node3.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>1 PEP 218: Built-In Set Objects</title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="Contents" | |
24 | href="contents.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="What's New in Python" | |
27 | href="whatsnew24.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="2 PEP 237: Unifying" | |
30 | href="node3.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">What's New in Python 2.4</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><img src='../icons/blank.png' | |
37 | border='0' height='32' alt='' width='32' /></td> | |
38 | <td class='online-navigation'><img src='../icons/blank.png' | |
39 | border='0' height='32' alt='' width='32' /></td> | |
40 | </tr></table> | |
41 | <div class='online-navigation'> | |
42 | <b class="navlabel">Previous:</b> | |
43 | <a class="sectref" rel="prev" href="contents.html">Contents</A> | |
44 | <b class="navlabel">Up:</b> | |
45 | <a class="sectref" rel="parent" href="whatsnew24.html">What's New in Python</A> | |
46 | <b class="navlabel">Next:</b> | |
47 | <a class="sectref" rel="next" href="node3.html">2 PEP 237: Unifying</A> | |
48 | </div> | |
49 | <hr /></div> | |
50 | </DIV> | |
51 | <!--End of Navigation Panel--> | |
52 | ||
53 | <H1><A NAME="SECTION000200000000000000000"> | |
54 | 1 PEP 218: Built-In Set Objects</A> | |
55 | </H1> | |
56 | ||
57 | <P> | |
58 | Python 2.3 introduced the <tt class="module">sets</tt> module. C implementations of | |
59 | set data types have now been added to the Python core as two new | |
60 | built-in types, <tt class="function">set(<var>iterable</var>)</tt> and | |
61 | <tt class="function">frozenset(<var>iterable</var>)</tt>. They provide high speed | |
62 | operations for membership testing, for eliminating duplicates from | |
63 | sequences, and for mathematical operations like unions, intersections, | |
64 | differences, and symmetric differences. | |
65 | ||
66 | <P> | |
67 | <div class="verbatim"><pre> | |
68 | >>> a = set('abracadabra') # form a set from a string | |
69 | >>> 'z' in a # fast membership testing | |
70 | False | |
71 | >>> a # unique letters in a | |
72 | set(['a', 'r', 'b', 'c', 'd']) | |
73 | >>> ''.join(a) # convert back into a string | |
74 | 'arbcd' | |
75 | ||
76 | >>> b = set('alacazam') # form a second set | |
77 | >>> a - b # letters in a but not in b | |
78 | set(['r', 'd', 'b']) | |
79 | >>> a | b # letters in either a or b | |
80 | set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l']) | |
81 | >>> a & b # letters in both a and b | |
82 | set(['a', 'c']) | |
83 | >>> a ^ b # letters in a or b but not both | |
84 | set(['r', 'd', 'b', 'm', 'z', 'l']) | |
85 | ||
86 | >>> a.add('z') # add a new element | |
87 | >>> a.update('wxy') # add multiple new elements | |
88 | >>> a | |
89 | set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'x', 'z']) | |
90 | >>> a.remove('x') # take one element out | |
91 | >>> a | |
92 | set(['a', 'c', 'b', 'd', 'r', 'w', 'y', 'z']) | |
93 | </pre></div> | |
94 | ||
95 | <P> | |
96 | The <tt class="function">frozenset</tt> type is an immutable version of <tt class="function">set</tt>. | |
97 | Since it is immutable and hashable, it may be used as a dictionary key or | |
98 | as a member of another set. | |
99 | ||
100 | <P> | |
101 | The <tt class="module">sets</tt> module remains in the standard library, and may be | |
102 | useful if you wish to subclass the <tt class="class">Set</tt> or <tt class="class">ImmutableSet</tt> | |
103 | classes. There are currently no plans to deprecate the module. | |
104 | ||
105 | <P> | |
106 | <div class="seealso"> | |
107 | <p class="heading">See Also:</p> | |
108 | ||
109 | <dl compact="compact" class="seerfc"> | |
110 | <dt><a href="http://www.python.org/peps/pep-0218.html" | |
111 | title="Adding a Built-In Set Object Type" | |
112 | >PEP 218, <em>Adding a Built-In Set Object Type</em></a> | |
113 | <dd>Originally proposed by | |
114 | Greg Wilson and ultimately implemented by Raymond Hettinger. | |
115 | </dl> | |
116 | </div> | |
117 | ||
118 | <P> | |
119 | ||
120 | <DIV CLASS="navigation"> | |
121 | <div class='online-navigation'> | |
122 | <p></p><hr /> | |
123 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
124 | <tr> | |
125 | <td class='online-navigation'><a rel="prev" title="Contents" | |
126 | href="contents.html"><img src='../icons/previous.png' | |
127 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
128 | <td class='online-navigation'><a rel="parent" title="What's New in Python" | |
129 | href="whatsnew24.html"><img src='../icons/up.png' | |
130 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
131 | <td class='online-navigation'><a rel="next" title="2 PEP 237: Unifying" | |
132 | href="node3.html"><img src='../icons/next.png' | |
133 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
134 | <td align="center" width="100%">What's New in Python 2.4</td> | |
135 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
136 | href="contents.html"><img src='../icons/contents.png' | |
137 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
138 | <td class='online-navigation'><img src='../icons/blank.png' | |
139 | border='0' height='32' alt='' width='32' /></td> | |
140 | <td class='online-navigation'><img src='../icons/blank.png' | |
141 | border='0' height='32' alt='' width='32' /></td> | |
142 | </tr></table> | |
143 | <div class='online-navigation'> | |
144 | <b class="navlabel">Previous:</b> | |
145 | <a class="sectref" rel="prev" href="contents.html">Contents</A> | |
146 | <b class="navlabel">Up:</b> | |
147 | <a class="sectref" rel="parent" href="whatsnew24.html">What's New in Python</A> | |
148 | <b class="navlabel">Next:</b> | |
149 | <a class="sectref" rel="next" href="node3.html">2 PEP 237: Unifying</A> | |
150 | </div> | |
151 | </div> | |
152 | <hr /> | |
153 | <span class="release-info">Release 1.01.</span> | |
154 | </DIV> | |
155 | <!--End of Navigation Panel--> | |
156 | <ADDRESS> | |
157 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
158 | </ADDRESS> | |
159 | </BODY> | |
160 | </HTML> |