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="tut.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="tut.html" title='Python Tutorial' /> | |
8 | <link rel='contents' href='node2.html' title="Contents" /> | |
9 | <link rel='index' href='node19.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="node8.html" /> | |
13 | <link rel="prev" href="node6.html" /> | |
14 | <link rel="parent" href="tut.html" /> | |
15 | <link rel="next" href="node8.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>5. Data Structures </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="4. More Control Flow" | |
25 | href="node6.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="Python Tutorial" | |
28 | href="tut.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="6. Modules" | |
31 | href="node8.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 Tutorial</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="node2.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><img src='../icons/blank.png' | |
38 | border='0' height='32' alt='' width='32' /></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="node19.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="node6.html">4. More Control Flow</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="tut.html">Python Tutorial</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="node8.html">6. Modules</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | <div class='online-navigation'> | |
55 | <!--Table of Child-Links--> | |
56 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
57 | ||
58 | <UL CLASS="ChildLinks"> | |
59 | <LI><A href="node7.html#SECTION007100000000000000000">5.1 More on Lists</a> | |
60 | <UL> | |
61 | <LI><A href="node7.html#SECTION007110000000000000000">5.1.1 Using Lists as Stacks</a> | |
62 | <LI><A href="node7.html#SECTION007120000000000000000">5.1.2 Using Lists as Queues</a> | |
63 | <LI><A href="node7.html#SECTION007130000000000000000">5.1.3 Functional Programming Tools</a> | |
64 | <LI><A href="node7.html#SECTION007140000000000000000">5.1.4 List Comprehensions</a> | |
65 | </ul> | |
66 | <LI><A href="node7.html#SECTION007200000000000000000">5.2 The <tt class="keyword">del</tt> statement</a> | |
67 | <LI><A href="node7.html#SECTION007300000000000000000">5.3 Tuples and Sequences</a> | |
68 | <LI><A href="node7.html#SECTION007400000000000000000">5.4 Sets</a> | |
69 | <LI><A href="node7.html#SECTION007500000000000000000">5.5 Dictionaries</a> | |
70 | <LI><A href="node7.html#SECTION007600000000000000000">5.6 Looping Techniques</a> | |
71 | <LI><A href="node7.html#SECTION007700000000000000000">5.7 More on Conditions</a> | |
72 | <LI><A href="node7.html#SECTION007800000000000000000">5.8 Comparing Sequences and Other Types</a> | |
73 | </ul> | |
74 | <!--End of Table of Child-Links--> | |
75 | </div> | |
76 | <HR> | |
77 | ||
78 | <H1><A NAME="SECTION007000000000000000000"></A><A NAME="structures"></A> | |
79 | <BR> | |
80 | 5. Data Structures | |
81 | </H1> | |
82 | ||
83 | <P> | |
84 | This chapter describes some things you've learned about already in | |
85 | more detail, and adds some new things as well. | |
86 | ||
87 | <P> | |
88 | ||
89 | <H1><A NAME="SECTION007100000000000000000"></A><A NAME="moreLists"></A> | |
90 | <BR> | |
91 | 5.1 More on Lists | |
92 | </H1> | |
93 | ||
94 | <P> | |
95 | The list data type has some more methods. Here are all of the methods | |
96 | of list objects: | |
97 | ||
98 | <P> | |
99 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
100 | <td><nobr><b><tt id='l2h-9' xml:id='l2h-9' class="method">append</tt></b>(</nobr></td> | |
101 | <td><var>x</var>)</td></tr></table></dt> | |
102 | <dd> | |
103 | Add an item to the end of the list; | |
104 | equivalent to <code>a[len(a):] = [<var>x</var>]</code>. | |
105 | </dl> | |
106 | ||
107 | <P> | |
108 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
109 | <td><nobr><b><tt id='l2h-10' xml:id='l2h-10' class="method">extend</tt></b>(</nobr></td> | |
110 | <td><var>L</var>)</td></tr></table></dt> | |
111 | <dd> | |
112 | Extend the list by appending all the items in the given list; | |
113 | equivalent to <code>a[len(a):] = <var>L</var></code>. | |
114 | </dl> | |
115 | ||
116 | <P> | |
117 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
118 | <td><nobr><b><tt id='l2h-11' xml:id='l2h-11' class="method">insert</tt></b>(</nobr></td> | |
119 | <td><var>i, x</var>)</td></tr></table></dt> | |
120 | <dd> | |
121 | Insert an item at a given position. The first argument is the index | |
122 | of the element before which to insert, so <code>a.insert(0, <var>x</var>)</code> | |
123 | inserts at the front of the list, and <code>a.insert(len(a), <var>x</var>)</code> | |
124 | is equivalent to <code>a.append(<var>x</var>)</code>. | |
125 | </dl> | |
126 | ||
127 | <P> | |
128 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
129 | <td><nobr><b><tt id='l2h-12' xml:id='l2h-12' class="method">remove</tt></b>(</nobr></td> | |
130 | <td><var>x</var>)</td></tr></table></dt> | |
131 | <dd> | |
132 | Remove the first item from the list whose value is <var>x</var>. | |
133 | It is an error if there is no such item. | |
134 | </dl> | |
135 | ||
136 | <P> | |
137 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
138 | <td><nobr><b><tt id='l2h-13' xml:id='l2h-13' class="method">pop</tt></b>(</nobr></td> | |
139 | <td><var></var><big>[</big><var>i</var><big>]</big><var></var>)</td></tr></table></dt> | |
140 | <dd> | |
141 | Remove the item at the given position in the list, and return it. If | |
142 | no index is specified, <code>a.pop()</code> removes and returns the last item | |
143 | in the list. The item is also removed from the list. (The square brackets | |
144 | around the <var>i</var> in the method signature denote that the parameter | |
145 | is optional, not that you should type square brackets at that | |
146 | position. You will see this notation frequently in the | |
147 | <em class="citetitle"><a | |
148 | href="../lib/lib.html" | |
149 | title="Python Library Reference" | |
150 | >Python Library Reference</a></em>.) | |
151 | </dl> | |
152 | ||
153 | <P> | |
154 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
155 | <td><nobr><b><tt id='l2h-14' xml:id='l2h-14' class="method">index</tt></b>(</nobr></td> | |
156 | <td><var>x</var>)</td></tr></table></dt> | |
157 | <dd> | |
158 | Return the index in the list of the first item whose value is <var>x</var>. | |
159 | It is an error if there is no such item. | |
160 | </dl> | |
161 | ||
162 | <P> | |
163 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
164 | <td><nobr><b><tt id='l2h-15' xml:id='l2h-15' class="method">count</tt></b>(</nobr></td> | |
165 | <td><var>x</var>)</td></tr></table></dt> | |
166 | <dd> | |
167 | Return the number of times <var>x</var> appears in the list. | |
168 | </dl> | |
169 | ||
170 | <P> | |
171 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
172 | <td><nobr><b><tt id='l2h-16' xml:id='l2h-16' class="method">sort</tt></b>(</nobr></td> | |
173 | <td><var></var>)</td></tr></table></dt> | |
174 | <dd> | |
175 | Sort the items of the list, in place. | |
176 | </dl> | |
177 | ||
178 | <P> | |
179 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
180 | <td><nobr><b><tt id='l2h-17' xml:id='l2h-17' class="method">reverse</tt></b>(</nobr></td> | |
181 | <td><var></var>)</td></tr></table></dt> | |
182 | <dd> | |
183 | Reverse the elements of the list, in place. | |
184 | </dl> | |
185 | ||
186 | <P> | |
187 | An example that uses most of the list methods: | |
188 | ||
189 | <P> | |
190 | <div class="verbatim"><pre> | |
191 | >>> a = [66.25, 333, 333, 1, 1234.5] | |
192 | >>> print a.count(333), a.count(66.25), a.count('x') | |
193 | 2 1 0 | |
194 | >>> a.insert(2, -1) | |
195 | >>> a.append(333) | |
196 | >>> a | |
197 | [66.25, 333, -1, 333, 1, 1234.5, 333] | |
198 | >>> a.index(333) | |
199 | 1 | |
200 | >>> a.remove(333) | |
201 | >>> a | |
202 | [66.25, -1, 333, 1, 1234.5, 333] | |
203 | >>> a.reverse() | |
204 | >>> a | |
205 | [333, 1234.5, 1, 333, -1, 66.25] | |
206 | >>> a.sort() | |
207 | >>> a | |
208 | [-1, 1, 66.25, 333, 333, 1234.5] | |
209 | </pre></div> | |
210 | ||
211 | <P> | |
212 | ||
213 | <H2><A NAME="SECTION007110000000000000000"></A><A NAME="lists-as-stacks"></A> | |
214 | <BR> | |
215 | 5.1.1 Using Lists as Stacks | |
216 | </H2> | |
217 | ||
218 | <P> | |
219 | The list methods make it very easy to use a list as a stack, where the | |
220 | last element added is the first element retrieved (``last-in, | |
221 | first-out''). To add an item to the top of the stack, use | |
222 | <tt class="method">append()</tt>. To retrieve an item from the top of the stack, use | |
223 | <tt class="method">pop()</tt> without an explicit index. For example: | |
224 | ||
225 | <P> | |
226 | <div class="verbatim"><pre> | |
227 | >>> stack = [3, 4, 5] | |
228 | >>> stack.append(6) | |
229 | >>> stack.append(7) | |
230 | >>> stack | |
231 | [3, 4, 5, 6, 7] | |
232 | >>> stack.pop() | |
233 | 7 | |
234 | >>> stack | |
235 | [3, 4, 5, 6] | |
236 | >>> stack.pop() | |
237 | 6 | |
238 | >>> stack.pop() | |
239 | 5 | |
240 | >>> stack | |
241 | [3, 4] | |
242 | </pre></div> | |
243 | ||
244 | <P> | |
245 | ||
246 | <H2><A NAME="SECTION007120000000000000000"></A><A NAME="lists-as-queues"></A> | |
247 | <BR> | |
248 | 5.1.2 Using Lists as Queues | |
249 | </H2> | |
250 | ||
251 | <P> | |
252 | You can also use a list conveniently as a queue, where the first | |
253 | element added is the first element retrieved (``first-in, | |
254 | first-out''). To add an item to the back of the queue, use | |
255 | <tt class="method">append()</tt>. To retrieve an item from the front of the queue, | |
256 | use <tt class="method">pop()</tt> with <code>0</code> as the index. For example: | |
257 | ||
258 | <P> | |
259 | <div class="verbatim"><pre> | |
260 | >>> queue = ["Eric", "John", "Michael"] | |
261 | >>> queue.append("Terry") # Terry arrives | |
262 | >>> queue.append("Graham") # Graham arrives | |
263 | >>> queue.pop(0) | |
264 | 'Eric' | |
265 | >>> queue.pop(0) | |
266 | 'John' | |
267 | >>> queue | |
268 | ['Michael', 'Terry', 'Graham'] | |
269 | </pre></div> | |
270 | ||
271 | <P> | |
272 | ||
273 | <H2><A NAME="SECTION007130000000000000000"></A><A NAME="functional"></A> | |
274 | <BR> | |
275 | 5.1.3 Functional Programming Tools | |
276 | </H2> | |
277 | ||
278 | <P> | |
279 | There are three built-in functions that are very useful when used with | |
280 | lists: <tt class="function">filter()</tt>, <tt class="function">map()</tt>, and <tt class="function">reduce()</tt>. | |
281 | ||
282 | <P> | |
283 | "<tt class="samp">filter(<var>function</var>, <var>sequence</var>)</tt>" returns a sequence | |
284 | consisting of those items from the | |
285 | sequence for which <code><var>function</var>(<var>item</var>)</code> is true. | |
286 | If <var>sequence</var> is a <tt class="class">string</tt> or <tt class="class">tuple</tt>, the result will | |
287 | be of the same type; otherwise, it is always a <tt class="class">list</tt>. | |
288 | For example, to compute some primes: | |
289 | ||
290 | <P> | |
291 | <div class="verbatim"><pre> | |
292 | >>> def f(x): return x % 2 != 0 and x % 3 != 0 | |
293 | ... | |
294 | >>> filter(f, range(2, 25)) | |
295 | [5, 7, 11, 13, 17, 19, 23] | |
296 | </pre></div> | |
297 | ||
298 | <P> | |
299 | "<tt class="samp">map(<var>function</var>, <var>sequence</var>)</tt>" calls | |
300 | <code><var>function</var>(<var>item</var>)</code> for each of the sequence's items and | |
301 | returns a list of the return values. For example, to compute some | |
302 | cubes: | |
303 | ||
304 | <P> | |
305 | <div class="verbatim"><pre> | |
306 | >>> def cube(x): return x*x*x | |
307 | ... | |
308 | >>> map(cube, range(1, 11)) | |
309 | [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000] | |
310 | </pre></div> | |
311 | ||
312 | <P> | |
313 | More than one sequence may be passed; the function must then have as | |
314 | many arguments as there are sequences and is called with the | |
315 | corresponding item from each sequence (or <code>None</code> if some sequence | |
316 | is shorter than another). For example: | |
317 | ||
318 | <P> | |
319 | <div class="verbatim"><pre> | |
320 | >>> seq = range(8) | |
321 | >>> def add(x, y): return x+y | |
322 | ... | |
323 | >>> map(add, seq, seq) | |
324 | [0, 2, 4, 6, 8, 10, 12, 14] | |
325 | </pre></div> | |
326 | ||
327 | <P> | |
328 | "<tt class="samp">reduce(<var>function</var>, <var>sequence</var>)</tt>" returns a single value | |
329 | constructed by calling the binary function <var>function</var> on the first two | |
330 | items of the sequence, then on the result and the next item, and so | |
331 | on. For example, to compute the sum of the numbers 1 through 10: | |
332 | ||
333 | <P> | |
334 | <div class="verbatim"><pre> | |
335 | >>> def add(x,y): return x+y | |
336 | ... | |
337 | >>> reduce(add, range(1, 11)) | |
338 | 55 | |
339 | </pre></div> | |
340 | ||
341 | <P> | |
342 | If there's only one item in the sequence, its value is returned; if | |
343 | the sequence is empty, an exception is raised. | |
344 | ||
345 | <P> | |
346 | A third argument can be passed to indicate the starting value. In this | |
347 | case the starting value is returned for an empty sequence, and the | |
348 | function is first applied to the starting value and the first sequence | |
349 | item, then to the result and the next item, and so on. For example, | |
350 | ||
351 | <P> | |
352 | <div class="verbatim"><pre> | |
353 | >>> def sum(seq): | |
354 | ... def add(x,y): return x+y | |
355 | ... return reduce(add, seq, 0) | |
356 | ... | |
357 | >>> sum(range(1, 11)) | |
358 | 55 | |
359 | >>> sum([]) | |
360 | 0 | |
361 | </pre></div> | |
362 | ||
363 | <P> | |
364 | Don't use this example's definition of <tt class="function">sum()</tt>: since summing | |
365 | numbers is such a common need, a built-in function | |
366 | <code>sum(<var>sequence</var>)</code> is already provided, and works exactly like | |
367 | this. | |
368 | ||
369 | <span class="versionnote">New in version 2.3.</span> | |
370 | ||
371 | <P> | |
372 | ||
373 | <H2><A NAME="SECTION007140000000000000000"> | |
374 | 5.1.4 List Comprehensions</A> | |
375 | </H2> | |
376 | ||
377 | <P> | |
378 | List comprehensions provide a concise way to create lists without resorting | |
379 | to use of <tt class="function">map()</tt>, <tt class="function">filter()</tt> and/or <tt class="keyword">lambda</tt>. | |
380 | The resulting list definition tends often to be clearer than lists built | |
381 | using those constructs. Each list comprehension consists of an expression | |
382 | followed by a <tt class="keyword">for</tt> clause, then zero or more <tt class="keyword">for</tt> or | |
383 | <tt class="keyword">if</tt> clauses. The result will be a list resulting from evaluating | |
384 | the expression in the context of the <tt class="keyword">for</tt> and <tt class="keyword">if</tt> clauses | |
385 | which follow it. If the expression would evaluate to a tuple, it must be | |
386 | parenthesized. | |
387 | ||
388 | <P> | |
389 | <div class="verbatim"><pre> | |
390 | >>> freshfruit = [' banana', ' loganberry ', 'passion fruit '] | |
391 | >>> [weapon.strip() for weapon in freshfruit] | |
392 | ['banana', 'loganberry', 'passion fruit'] | |
393 | >>> vec = [2, 4, 6] | |
394 | >>> [3*x for x in vec] | |
395 | [6, 12, 18] | |
396 | >>> [3*x for x in vec if x > 3] | |
397 | [12, 18] | |
398 | >>> [3*x for x in vec if x < 2] | |
399 | [] | |
400 | >>> [[x,x**2] for x in vec] | |
401 | [[2, 4], [4, 16], [6, 36]] | |
402 | >>> [x, x**2 for x in vec] # error - parens required for tuples | |
403 | File "<stdin>", line 1, in ? | |
404 | [x, x**2 for x in vec] | |
405 | ^ | |
406 | SyntaxError: invalid syntax | |
407 | >>> [(x, x**2) for x in vec] | |
408 | [(2, 4), (4, 16), (6, 36)] | |
409 | >>> vec1 = [2, 4, 6] | |
410 | >>> vec2 = [4, 3, -9] | |
411 | >>> [x*y for x in vec1 for y in vec2] | |
412 | [8, 6, -18, 16, 12, -36, 24, 18, -54] | |
413 | >>> [x+y for x in vec1 for y in vec2] | |
414 | [6, 5, -7, 8, 7, -5, 10, 9, -3] | |
415 | >>> [vec1[i]*vec2[i] for i in range(len(vec1))] | |
416 | [8, 12, -54] | |
417 | </pre></div> | |
418 | ||
419 | <P> | |
420 | List comprehensions are much more flexible than <tt class="function">map()</tt> and can be | |
421 | applied to complex expressions and nested functions: | |
422 | ||
423 | <P> | |
424 | <div class="verbatim"><pre> | |
425 | >>> [str(round(355/113.0, i)) for i in range(1,6)] | |
426 | ['3.1', '3.14', '3.142', '3.1416', '3.14159'] | |
427 | </pre></div> | |
428 | ||
429 | <P> | |
430 | ||
431 | <H1><A NAME="SECTION007200000000000000000"></A><A NAME="del"></A> | |
432 | <BR> | |
433 | 5.2 The <tt class="keyword">del</tt> statement | |
434 | </H1> | |
435 | ||
436 | <P> | |
437 | There is a way to remove an item from a list given its index instead | |
438 | of its value: the <tt class="keyword">del</tt> statement. Unlike the <tt class="method">pop()</tt>) | |
439 | method which returns a value, the <tt class="keyword">del</tt> keyword is a statement | |
440 | and can also be used to | |
441 | remove slices from a list (which we did earlier by assignment of an | |
442 | empty list to the slice). For example: | |
443 | ||
444 | <P> | |
445 | <div class="verbatim"><pre> | |
446 | >>> a = [-1, 1, 66.25, 333, 333, 1234.5] | |
447 | >>> del a[0] | |
448 | >>> a | |
449 | [1, 66.25, 333, 333, 1234.5] | |
450 | >>> del a[2:4] | |
451 | >>> a | |
452 | [1, 66.25, 1234.5] | |
453 | </pre></div> | |
454 | ||
455 | <P> | |
456 | <tt class="keyword">del</tt> can also be used to delete entire variables: | |
457 | ||
458 | <P> | |
459 | <div class="verbatim"><pre> | |
460 | >>> del a | |
461 | </pre></div> | |
462 | ||
463 | <P> | |
464 | Referencing the name <code>a</code> hereafter is an error (at least until | |
465 | another value is assigned to it). We'll find other uses for | |
466 | <tt class="keyword">del</tt> later. | |
467 | ||
468 | <P> | |
469 | ||
470 | <H1><A NAME="SECTION007300000000000000000"></A><A NAME="tuples"></A> | |
471 | <BR> | |
472 | 5.3 Tuples and Sequences | |
473 | </H1> | |
474 | ||
475 | <P> | |
476 | We saw that lists and strings have many common properties, such as | |
477 | indexing and slicing operations. They are two examples of | |
478 | <a class="ulink" href="../lib/typesseq.html" | |
479 | ><em>sequence</em> data types</a>. Since | |
480 | Python is an evolving language, other sequence data types may be | |
481 | added. There is also another standard sequence data type: the | |
482 | <em>tuple</em>. | |
483 | ||
484 | <P> | |
485 | A tuple consists of a number of values separated by commas, for | |
486 | instance: | |
487 | ||
488 | <P> | |
489 | <div class="verbatim"><pre> | |
490 | >>> t = 12345, 54321, 'hello!' | |
491 | >>> t[0] | |
492 | 12345 | |
493 | >>> t | |
494 | (12345, 54321, 'hello!') | |
495 | >>> # Tuples may be nested: | |
496 | ... u = t, (1, 2, 3, 4, 5) | |
497 | >>> u | |
498 | ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5)) | |
499 | </pre></div> | |
500 | ||
501 | <P> | |
502 | As you see, on output tuples are always enclosed in parentheses, so | |
503 | that nested tuples are interpreted correctly; they may be input with | |
504 | or without surrounding parentheses, although often parentheses are | |
505 | necessary anyway (if the tuple is part of a larger expression). | |
506 | ||
507 | <P> | |
508 | Tuples have many uses. For example: (x, y) coordinate pairs, employee | |
509 | records from a database, etc. Tuples, like strings, are immutable: it | |
510 | is not possible to assign to the individual items of a tuple (you can | |
511 | simulate much of the same effect with slicing and concatenation, | |
512 | though). It is also possible to create tuples which contain mutable | |
513 | objects, such as lists. | |
514 | ||
515 | <P> | |
516 | A special problem is the construction of tuples containing 0 or 1 | |
517 | items: the syntax has some extra quirks to accommodate these. Empty | |
518 | tuples are constructed by an empty pair of parentheses; a tuple with | |
519 | one item is constructed by following a value with a comma | |
520 | (it is not sufficient to enclose a single value in parentheses). | |
521 | Ugly, but effective. For example: | |
522 | ||
523 | <P> | |
524 | <div class="verbatim"><pre> | |
525 | >>> empty = () | |
526 | >>> singleton = 'hello', # <-- note trailing comma | |
527 | >>> len(empty) | |
528 | 0 | |
529 | >>> len(singleton) | |
530 | 1 | |
531 | >>> singleton | |
532 | ('hello',) | |
533 | </pre></div> | |
534 | ||
535 | <P> | |
536 | The statement <code>t = 12345, 54321, 'hello!'</code> is an example of | |
537 | <em>tuple packing</em>: the values <code>12345</code>, <code>54321</code> and | |
538 | <code>'hello!'</code> are packed together in a tuple. The reverse operation | |
539 | is also possible: | |
540 | ||
541 | <P> | |
542 | <div class="verbatim"><pre> | |
543 | >>> x, y, z = t | |
544 | </pre></div> | |
545 | ||
546 | <P> | |
547 | This is called, appropriately enough, <em>sequence unpacking</em>. | |
548 | Sequence unpacking requires the list of variables on the left to | |
549 | have the same number of elements as the length of the sequence. Note | |
550 | that multiple assignment is really just a combination of tuple packing | |
551 | and sequence unpacking! | |
552 | ||
553 | <P> | |
554 | There is a small bit of asymmetry here: packing multiple values | |
555 | always creates a tuple, and unpacking works for any sequence. | |
556 | ||
557 | <P> | |
558 | ||
559 | <H1><A NAME="SECTION007400000000000000000"></A><A NAME="sets"></A> | |
560 | <BR> | |
561 | 5.4 Sets | |
562 | </H1> | |
563 | ||
564 | <P> | |
565 | Python also includes a data type for <em>sets</em>. A set is an unordered | |
566 | collection with no duplicate elements. Basic uses include membership | |
567 | testing and eliminating duplicate entries. Set objects also support | |
568 | mathematical operations like union, intersection, difference, and | |
569 | symmetric difference. | |
570 | ||
571 | <P> | |
572 | Here is a brief demonstration: | |
573 | ||
574 | <P> | |
575 | <div class="verbatim"><pre> | |
576 | >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] | |
577 | >>> fruit = set(basket) # create a set without duplicates | |
578 | >>> fruit | |
579 | set(['orange', 'pear', 'apple', 'banana']) | |
580 | >>> 'orange' in fruit # fast membership testing | |
581 | True | |
582 | >>> 'crabgrass' in fruit | |
583 | False | |
584 | ||
585 | >>> # Demonstrate set operations on unique letters from two words | |
586 | ... | |
587 | >>> a = set('abracadabra') | |
588 | >>> b = set('alacazam') | |
589 | >>> a # unique letters in a | |
590 | set(['a', 'r', 'b', 'c', 'd']) | |
591 | >>> a - b # letters in a but not in b | |
592 | set(['r', 'd', 'b']) | |
593 | >>> a | b # letters in either a or b | |
594 | set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l']) | |
595 | >>> a & b # letters in both a and b | |
596 | set(['a', 'c']) | |
597 | >>> a ^ b # letters in a or b but not both | |
598 | set(['r', 'd', 'b', 'm', 'z', 'l']) | |
599 | </pre></div> | |
600 | ||
601 | <P> | |
602 | ||
603 | <H1><A NAME="SECTION007500000000000000000"></A><A NAME="dictionaries"></A> | |
604 | <BR> | |
605 | 5.5 Dictionaries | |
606 | </H1> | |
607 | ||
608 | <P> | |
609 | Another useful data type built into Python is the | |
610 | <a class="ulink" href="../lib/typesmapping.html" | |
611 | ><em>dictionary</em></a>. | |
612 | Dictionaries are sometimes found in other languages as ``associative | |
613 | memories'' or ``associative arrays''. Unlike sequences, which are | |
614 | indexed by a range of numbers, dictionaries are indexed by <em>keys</em>, | |
615 | which can be any immutable type; strings and numbers can always be | |
616 | keys. Tuples can be used as keys if they contain only strings, | |
617 | numbers, or tuples; if a tuple contains any mutable object either | |
618 | directly or indirectly, it cannot be used as a key. You can't use | |
619 | lists as keys, since lists can be modified in place using methods like | |
620 | <tt class="method">append()</tt> and <tt class="method">extend()</tt> or modified with slice and | |
621 | indexed assignments. | |
622 | ||
623 | <P> | |
624 | It is best to think of a dictionary as an unordered set of | |
625 | <em>key: value</em> pairs, with the requirement that the keys are unique | |
626 | (within one dictionary). | |
627 | A pair of braces creates an empty dictionary: <code>{}</code>. | |
628 | Placing a comma-separated list of key:value pairs within the | |
629 | braces adds initial key:value pairs to the dictionary; this is also the | |
630 | way dictionaries are written on output. | |
631 | ||
632 | <P> | |
633 | The main operations on a dictionary are storing a value with some key | |
634 | and extracting the value given the key. It is also possible to delete | |
635 | a key:value pair | |
636 | with <code>del</code>. | |
637 | If you store using a key that is already in use, the old value | |
638 | associated with that key is forgotten. It is an error to extract a | |
639 | value using a non-existent key. | |
640 | ||
641 | <P> | |
642 | The <tt class="method">keys()</tt> method of a dictionary object returns a list of all | |
643 | the keys used in the dictionary, in arbitrary order (if you want it | |
644 | sorted, just apply the <tt class="method">sort()</tt> method to the list of keys). To | |
645 | check whether a single key is in the dictionary, either use the dictionary's | |
646 | <tt class="method">has_key()</tt> method or the <tt class="keyword">in</tt> keyword. | |
647 | ||
648 | <P> | |
649 | Here is a small example using a dictionary: | |
650 | ||
651 | <P> | |
652 | <div class="verbatim"><pre> | |
653 | >>> tel = {'jack': 4098, 'sape': 4139} | |
654 | >>> tel['guido'] = 4127 | |
655 | >>> tel | |
656 | {'sape': 4139, 'guido': 4127, 'jack': 4098} | |
657 | >>> tel['jack'] | |
658 | 4098 | |
659 | >>> del tel['sape'] | |
660 | >>> tel['irv'] = 4127 | |
661 | >>> tel | |
662 | {'guido': 4127, 'irv': 4127, 'jack': 4098} | |
663 | >>> tel.keys() | |
664 | ['guido', 'irv', 'jack'] | |
665 | >>> tel.has_key('guido') | |
666 | True | |
667 | >>> 'guido' in tel | |
668 | True | |
669 | </pre></div> | |
670 | ||
671 | <P> | |
672 | The <tt class="function">dict()</tt> constructor builds dictionaries directly from | |
673 | lists of key-value pairs stored as tuples. When the pairs form a | |
674 | pattern, list comprehensions can compactly specify the key-value list. | |
675 | ||
676 | <P> | |
677 | <div class="verbatim"><pre> | |
678 | >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]) | |
679 | {'sape': 4139, 'jack': 4098, 'guido': 4127} | |
680 | >>> dict([(x, x**2) for x in (2, 4, 6)]) # use a list comprehension | |
681 | {2: 4, 4: 16, 6: 36} | |
682 | </pre></div> | |
683 | ||
684 | <P> | |
685 | Later in the tutorial, we will learn about Generator Expressions | |
686 | which are even better suited for the task of supplying key-values pairs to | |
687 | the <tt class="function">dict()</tt> constructor. | |
688 | ||
689 | <P> | |
690 | When the keys are simple strings, it is sometimes easier to specify | |
691 | pairs using keyword arguments: | |
692 | ||
693 | <P> | |
694 | <div class="verbatim"><pre> | |
695 | >>> dict(sape=4139, guido=4127, jack=4098) | |
696 | {'sape': 4139, 'jack': 4098, 'guido': 4127} | |
697 | </pre></div> | |
698 | ||
699 | <P> | |
700 | ||
701 | <H1><A NAME="SECTION007600000000000000000"></A><A NAME="loopidioms"></A> | |
702 | <BR> | |
703 | 5.6 Looping Techniques | |
704 | </H1> | |
705 | ||
706 | <P> | |
707 | When looping through dictionaries, the key and corresponding value can | |
708 | be retrieved at the same time using the <tt class="method">iteritems()</tt> method. | |
709 | ||
710 | <P> | |
711 | <div class="verbatim"><pre> | |
712 | >>> knights = {'gallahad': 'the pure', 'robin': 'the brave'} | |
713 | >>> for k, v in knights.iteritems(): | |
714 | ... print k, v | |
715 | ... | |
716 | gallahad the pure | |
717 | robin the brave | |
718 | </pre></div> | |
719 | ||
720 | <P> | |
721 | When looping through a sequence, the position index and corresponding | |
722 | value can be retrieved at the same time using the | |
723 | <tt class="function">enumerate()</tt> function. | |
724 | ||
725 | <P> | |
726 | <div class="verbatim"><pre> | |
727 | >>> for i, v in enumerate(['tic', 'tac', 'toe']): | |
728 | ... print i, v | |
729 | ... | |
730 | 0 tic | |
731 | 1 tac | |
732 | 2 toe | |
733 | </pre></div> | |
734 | ||
735 | <P> | |
736 | To loop over two or more sequences at the same time, the entries | |
737 | can be paired with the <tt class="function">zip()</tt> function. | |
738 | ||
739 | <P> | |
740 | <div class="verbatim"><pre> | |
741 | >>> questions = ['name', 'quest', 'favorite color'] | |
742 | >>> answers = ['lancelot', 'the holy grail', 'blue'] | |
743 | >>> for q, a in zip(questions, answers): | |
744 | ... print 'What is your %s? It is %s.' % (q, a) | |
745 | ... | |
746 | What is your name? It is lancelot. | |
747 | What is your quest? It is the holy grail. | |
748 | What is your favorite color? It is blue. | |
749 | </pre></div> | |
750 | ||
751 | <P> | |
752 | To loop over a sequence in reverse, first specify the sequence | |
753 | in a forward direction and then call the <tt class="function">reversed()</tt> | |
754 | function. | |
755 | ||
756 | <P> | |
757 | <div class="verbatim"><pre> | |
758 | >>> for i in reversed(xrange(1,10,2)): | |
759 | ... print i | |
760 | ... | |
761 | 9 | |
762 | 7 | |
763 | 5 | |
764 | 3 | |
765 | 1 | |
766 | </pre></div> | |
767 | ||
768 | <P> | |
769 | To loop over a sequence in sorted order, use the <tt class="function">sorted()</tt> | |
770 | function which returns a new sorted list while leaving the source | |
771 | unaltered. | |
772 | ||
773 | <P> | |
774 | <div class="verbatim"><pre> | |
775 | >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] | |
776 | >>> for f in sorted(set(basket)): | |
777 | ... print f | |
778 | ... | |
779 | apple | |
780 | banana | |
781 | orange | |
782 | pear | |
783 | </pre></div> | |
784 | ||
785 | <P> | |
786 | ||
787 | <H1><A NAME="SECTION007700000000000000000"></A><A NAME="conditions"></A> | |
788 | <BR> | |
789 | 5.7 More on Conditions | |
790 | </H1> | |
791 | ||
792 | <P> | |
793 | The conditions used in <code>while</code> and <code>if</code> statements can | |
794 | contain any operators, not just comparisons. | |
795 | ||
796 | <P> | |
797 | The comparison operators <code>in</code> and <code>not in</code> check whether a value | |
798 | occurs (does not occur) in a sequence. The operators <code>is</code> and | |
799 | <code>is not</code> compare whether two objects are really the same object; this | |
800 | only matters for mutable objects like lists. All comparison operators | |
801 | have the same priority, which is lower than that of all numerical | |
802 | operators. | |
803 | ||
804 | <P> | |
805 | Comparisons can be chained. For example, <code>a < b == c</code> tests | |
806 | whether <code>a</code> is less than <code>b</code> and moreover <code>b</code> equals | |
807 | <code>c</code>. | |
808 | ||
809 | <P> | |
810 | Comparisons may be combined using the Boolean operators <code>and</code> and | |
811 | <code>or</code>, and the outcome of a comparison (or of any other Boolean | |
812 | expression) may be negated with <code>not</code>. These have lower | |
813 | priorities than comparison operators; between them, <code>not</code> has | |
814 | the highest priority and <code>or</code> the lowest, so that | |
815 | <code>A and not B or C</code> is equivalent to <code>(A and (not B)) or C</code>. | |
816 | As always, parentheses can be used to express the desired composition. | |
817 | ||
818 | <P> | |
819 | The Boolean operators <code>and</code> and <code>or</code> are so-called | |
820 | <em>short-circuit</em> operators: their arguments are evaluated from | |
821 | left to right, and evaluation stops as soon as the outcome is | |
822 | determined. For example, if <code>A</code> and <code>C</code> are true but | |
823 | <code>B</code> is false, <code>A and B and C</code> does not evaluate the | |
824 | expression <code>C</code>. When used as a general value and not as a | |
825 | Boolean, the return value of a short-circuit operator is the last | |
826 | evaluated argument. | |
827 | ||
828 | <P> | |
829 | It is possible to assign the result of a comparison or other Boolean | |
830 | expression to a variable. For example, | |
831 | ||
832 | <P> | |
833 | <div class="verbatim"><pre> | |
834 | >>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance' | |
835 | >>> non_null = string1 or string2 or string3 | |
836 | >>> non_null | |
837 | 'Trondheim' | |
838 | </pre></div> | |
839 | ||
840 | <P> | |
841 | Note that in Python, unlike C, assignment cannot occur inside expressions. | |
842 | C programmers may grumble about this, but it avoids a common class of | |
843 | problems encountered in C programs: typing <code>=</code> in an expression when | |
844 | <code>==</code> was intended. | |
845 | ||
846 | <P> | |
847 | ||
848 | <H1><A NAME="SECTION007800000000000000000"></A><A NAME="comparing"></A> | |
849 | <BR> | |
850 | 5.8 Comparing Sequences and Other Types | |
851 | </H1> | |
852 | ||
853 | <P> | |
854 | Sequence objects may be compared to other objects with the same | |
855 | sequence type. The comparison uses <em>lexicographical</em> ordering: | |
856 | first the first two items are compared, and if they differ this | |
857 | determines the outcome of the comparison; if they are equal, the next | |
858 | two items are compared, and so on, until either sequence is exhausted. | |
859 | If two items to be compared are themselves sequences of the same type, | |
860 | the lexicographical comparison is carried out recursively. If all | |
861 | items of two sequences compare equal, the sequences are considered | |
862 | equal. If one sequence is an initial sub-sequence of the other, the | |
863 | shorter sequence is the smaller (lesser) one. Lexicographical | |
864 | ordering for strings uses the ASCII ordering for individual | |
865 | characters. Some examples of comparisons between sequences of the | |
866 | same type: | |
867 | ||
868 | <P> | |
869 | <div class="verbatim"><pre> | |
870 | (1, 2, 3) < (1, 2, 4) | |
871 | [1, 2, 3] < [1, 2, 4] | |
872 | 'ABC' < 'C' < 'Pascal' < 'Python' | |
873 | (1, 2, 3, 4) < (1, 2, 4) | |
874 | (1, 2) < (1, 2, -1) | |
875 | (1, 2, 3) == (1.0, 2.0, 3.0) | |
876 | (1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4) | |
877 | </pre></div> | |
878 | ||
879 | <P> | |
880 | Note that comparing objects of different types is legal. The outcome | |
881 | is deterministic but arbitrary: the types are ordered by their name. | |
882 | Thus, a list is always smaller than a string, a string is always | |
883 | smaller than a tuple, etc. <A NAME="tex2html3" | |
884 | HREF="#foot736"><SUP>5.1</SUP></A> Mixed numeric types are compared according to their numeric value, so | |
885 | 0 equals 0.0, etc. | |
886 | ||
887 | <P> | |
888 | <BR><HR><H4>Footnotes</H4> | |
889 | <DL> | |
890 | <DT><A NAME="foot736">... etc.</A><A | |
891 | HREF="node7.html#tex2html3"><SUP>5.1</SUP></A></DT> | |
892 | <DD> | |
893 | The rules for comparing objects of different types should | |
894 | not be relied upon; they may change in a future version of | |
895 | the language. | |
896 | ||
897 | ||
898 | </DD> | |
899 | </DL> | |
900 | <DIV CLASS="navigation"> | |
901 | <div class='online-navigation'> | |
902 | <p></p><hr /> | |
903 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
904 | <tr> | |
905 | <td class='online-navigation'><a rel="prev" title="4. More Control Flow" | |
906 | href="node6.html"><img src='../icons/previous.png' | |
907 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
908 | <td class='online-navigation'><a rel="parent" title="Python Tutorial" | |
909 | href="tut.html"><img src='../icons/up.png' | |
910 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
911 | <td class='online-navigation'><a rel="next" title="6. Modules" | |
912 | href="node8.html"><img src='../icons/next.png' | |
913 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
914 | <td align="center" width="100%">Python Tutorial</td> | |
915 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
916 | href="node2.html"><img src='../icons/contents.png' | |
917 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
918 | <td class='online-navigation'><img src='../icons/blank.png' | |
919 | border='0' height='32' alt='' width='32' /></td> | |
920 | <td class='online-navigation'><a rel="index" title="Index" | |
921 | href="node19.html"><img src='../icons/index.png' | |
922 | border='0' height='32' alt='Index' width='32' /></A></td> | |
923 | </tr></table> | |
924 | <div class='online-navigation'> | |
925 | <b class="navlabel">Previous:</b> | |
926 | <a class="sectref" rel="prev" href="node6.html">4. More Control Flow</A> | |
927 | <b class="navlabel">Up:</b> | |
928 | <a class="sectref" rel="parent" href="tut.html">Python Tutorial</A> | |
929 | <b class="navlabel">Next:</b> | |
930 | <a class="sectref" rel="next" href="node8.html">6. Modules</A> | |
931 | </div> | |
932 | </div> | |
933 | <hr /> | |
934 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
935 | </DIV> | |
936 | <!--End of Navigation Panel--> | |
937 | <ADDRESS> | |
938 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
939 | </ADDRESS> | |
940 | </BODY> | |
941 | </HTML> |