Mercurial > cpython-lru_cache-weakref
annotate Doc/tutorial/datastructures.rst @ 46948:df11aff092cd
Remove duplicated paragraph.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Mon, 12 May 2008 16:33:11 +0000 |
| parents | 376b5cb8a014 |
| children | 55d8c35322a7 |
| rev | line source |
|---|---|
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
1 .. _tut-structures: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
2 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
3 *************** |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
4 Data Structures |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
5 *************** |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
6 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
7 This chapter describes some things you've learned about already in more detail, |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
8 and adds some new things as well. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
9 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
10 .. _tut-morelists: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
11 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
12 More on Lists |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
13 ============= |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
14 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
15 The list data type has some more methods. Here are all of the methods of list |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
16 objects: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
17 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
18 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
19 .. method:: list.append(x) |
|
45996
376b5cb8a014
Merged revisions 61687-61688,61696,61700,61704-61705,61707-61709,61711-61712,61714-61716,61718-61722 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45084
diff
changeset
|
20 :noindex: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
21 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
22 Add an item to the end of the list; equivalent to ``a[len(a):] = [x]``. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
23 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
24 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
25 .. method:: list.extend(L) |
|
45996
376b5cb8a014
Merged revisions 61687-61688,61696,61700,61704-61705,61707-61709,61711-61712,61714-61716,61718-61722 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45084
diff
changeset
|
26 :noindex: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
27 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
28 Extend the list by appending all the items in the given list; equivalent to |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
29 ``a[len(a):] = L``. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
30 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
31 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
32 .. method:: list.insert(i, x) |
|
45996
376b5cb8a014
Merged revisions 61687-61688,61696,61700,61704-61705,61707-61709,61711-61712,61714-61716,61718-61722 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45084
diff
changeset
|
33 :noindex: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
34 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
35 Insert an item at a given position. The first argument is the index of the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
36 element before which to insert, so ``a.insert(0, x)`` inserts at the front of |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
37 the list, and ``a.insert(len(a), x)`` is equivalent to ``a.append(x)``. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
38 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
39 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
40 .. method:: list.remove(x) |
|
45996
376b5cb8a014
Merged revisions 61687-61688,61696,61700,61704-61705,61707-61709,61711-61712,61714-61716,61718-61722 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45084
diff
changeset
|
41 :noindex: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
42 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
43 Remove the first item from the list whose value is *x*. It is an error if there |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
44 is no such item. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
45 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
46 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
47 .. method:: list.pop([i]) |
|
45996
376b5cb8a014
Merged revisions 61687-61688,61696,61700,61704-61705,61707-61709,61711-61712,61714-61716,61718-61722 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45084
diff
changeset
|
48 :noindex: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
49 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
50 Remove the item at the given position in the list, and return it. If no index |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
51 is specified, ``a.pop()`` removes and returns the last item in the list. (The |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
52 square brackets around the *i* in the method signature denote that the parameter |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
53 is optional, not that you should type square brackets at that position. You |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
54 will see this notation frequently in the Python Library Reference.) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
55 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
56 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
57 .. method:: list.index(x) |
|
45996
376b5cb8a014
Merged revisions 61687-61688,61696,61700,61704-61705,61707-61709,61711-61712,61714-61716,61718-61722 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45084
diff
changeset
|
58 :noindex: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
59 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
60 Return the index in the list of the first item whose value is *x*. It is an |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
61 error if there is no such item. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
62 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
63 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
64 .. method:: list.count(x) |
|
45996
376b5cb8a014
Merged revisions 61687-61688,61696,61700,61704-61705,61707-61709,61711-61712,61714-61716,61718-61722 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45084
diff
changeset
|
65 :noindex: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
66 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
67 Return the number of times *x* appears in the list. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
68 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
69 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
70 .. method:: list.sort() |
|
45996
376b5cb8a014
Merged revisions 61687-61688,61696,61700,61704-61705,61707-61709,61711-61712,61714-61716,61718-61722 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45084
diff
changeset
|
71 :noindex: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
72 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
73 Sort the items of the list, in place. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
74 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
75 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
76 .. method:: list.reverse() |
|
45996
376b5cb8a014
Merged revisions 61687-61688,61696,61700,61704-61705,61707-61709,61711-61712,61714-61716,61718-61722 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
45084
diff
changeset
|
77 :noindex: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
78 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
79 Reverse the elements of the list, in place. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
80 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
81 An example that uses most of the list methods:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
82 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
83 >>> a = [66.25, 333, 333, 1, 1234.5] |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
84 >>> print(a.count(333), a.count(66.25), a.count('x')) |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
85 2 1 0 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
86 >>> a.insert(2, -1) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
87 >>> a.append(333) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
88 >>> a |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
89 [66.25, 333, -1, 333, 1, 1234.5, 333] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
90 >>> a.index(333) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
91 1 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
92 >>> a.remove(333) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
93 >>> a |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
94 [66.25, -1, 333, 1, 1234.5, 333] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
95 >>> a.reverse() |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
96 >>> a |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
97 [333, 1234.5, 1, 333, -1, 66.25] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
98 >>> a.sort() |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
99 >>> a |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
100 [-1, 1, 66.25, 333, 333, 1234.5] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
101 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
102 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
103 .. _tut-lists-as-stacks: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
104 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
105 Using Lists as Stacks |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
106 --------------------- |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
107 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
108 .. sectionauthor:: Ka-Ping Yee <ping@lfw.org> |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
109 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
110 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
111 The list methods make it very easy to use a list as a stack, where the last |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
112 element added is the first element retrieved ("last-in, first-out"). To add an |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
113 item to the top of the stack, use :meth:`append`. To retrieve an item from the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
114 top of the stack, use :meth:`pop` without an explicit index. For example:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
115 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
116 >>> stack = [3, 4, 5] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
117 >>> stack.append(6) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
118 >>> stack.append(7) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
119 >>> stack |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
120 [3, 4, 5, 6, 7] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
121 >>> stack.pop() |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
122 7 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
123 >>> stack |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
124 [3, 4, 5, 6] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
125 >>> stack.pop() |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
126 6 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
127 >>> stack.pop() |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
128 5 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
129 >>> stack |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
130 [3, 4] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
131 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
132 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
133 .. _tut-lists-as-queues: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
134 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
135 Using Lists as Queues |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
136 --------------------- |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
137 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
138 .. sectionauthor:: Ka-Ping Yee <ping@lfw.org> |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
139 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
140 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
141 You can also use a list conveniently as a queue, where the first element added |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
142 is the first element retrieved ("first-in, first-out"). To add an item to the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
143 back of the queue, use :meth:`append`. To retrieve an item from the front of |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
144 the queue, use :meth:`pop` with ``0`` as the index. For example:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
145 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
146 >>> queue = ["Eric", "John", "Michael"] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
147 >>> queue.append("Terry") # Terry arrives |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
148 >>> queue.append("Graham") # Graham arrives |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
149 >>> queue.pop(0) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
150 'Eric' |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
151 >>> queue.pop(0) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
152 'John' |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
153 >>> queue |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
154 ['Michael', 'Terry', 'Graham'] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
155 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
156 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
157 List Comprehensions |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
158 ------------------- |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
159 |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
160 List comprehensions provide a concise way to create lists from sequences. |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
161 Common applications are to make lists where each element is the result of |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
162 some operations applied to each member of the sequence, or to create a |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
163 subsequence of those elements that satisfy a certain condition. |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
164 |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
165 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
166 Each list comprehension consists of an expression followed by a :keyword:`for` |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
167 clause, then zero or more :keyword:`for` or :keyword:`if` clauses. The result |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
168 will be a list resulting from evaluating the expression in the context of the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
169 :keyword:`for` and :keyword:`if` clauses which follow it. If the expression |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
170 would evaluate to a tuple, it must be parenthesized. |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
171 |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
172 Here we take a list of numbers and return a list of three times each number:: |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
173 |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
174 >>> vec = [2, 4, 6] |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
175 >>> [3*x for x in vec] |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
176 [6, 12, 18] |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
177 |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
178 Now we get a little fancier:: |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
179 |
|
43201
4902671dd678
Tutorial formatting patch by Robin Stocker.
Georg Brandl <georg@python.org>
parents:
43117
diff
changeset
|
180 >>> [[x, x**2] for x in vec] |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
181 [[2, 4], [4, 16], [6, 36]] |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
182 |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
183 Here we apply a method call to each item in a sequence:: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
184 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
185 >>> freshfruit = [' banana', ' loganberry ', 'passion fruit '] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
186 >>> [weapon.strip() for weapon in freshfruit] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
187 ['banana', 'loganberry', 'passion fruit'] |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
188 |
|
43201
4902671dd678
Tutorial formatting patch by Robin Stocker.
Georg Brandl <georg@python.org>
parents:
43117
diff
changeset
|
189 Using the :keyword:`if` clause we can filter the stream:: |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
190 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
191 >>> [3*x for x in vec if x > 3] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
192 [12, 18] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
193 >>> [3*x for x in vec if x < 2] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
194 [] |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
195 |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
196 Tuples can often be created without their parentheses, but not here:: |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
197 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
198 >>> [x, x**2 for x in vec] # error - parens required for tuples |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
199 File "<stdin>", line 1, in ? |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
200 [x, x**2 for x in vec] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
201 ^ |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
202 SyntaxError: invalid syntax |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
203 >>> [(x, x**2) for x in vec] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
204 [(2, 4), (4, 16), (6, 36)] |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
205 |
|
43201
4902671dd678
Tutorial formatting patch by Robin Stocker.
Georg Brandl <georg@python.org>
parents:
43117
diff
changeset
|
206 Here are some nested for loops and other fancy behavior:: |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
207 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
208 >>> vec1 = [2, 4, 6] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
209 >>> vec2 = [4, 3, -9] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
210 >>> [x*y for x in vec1 for y in vec2] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
211 [8, 6, -18, 16, 12, -36, 24, 18, -54] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
212 >>> [x+y for x in vec1 for y in vec2] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
213 [6, 5, -7, 8, 7, -5, 10, 9, -3] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
214 >>> [vec1[i]*vec2[i] for i in range(len(vec1))] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
215 [8, 12, -54] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
216 |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
217 List comprehensions can be applied to complex expressions and nested functions:: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
218 |
|
45084
38604fa2a48f
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
Georg Brandl <georg@python.org>
parents:
44396
diff
changeset
|
219 >>> [str(round(355/113, i)) for i in range(1, 6)] |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
220 ['3.1', '3.14', '3.142', '3.1416', '3.14159'] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
221 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
222 |
|
44310
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
223 Nested List Comprehensions |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
224 -------------------------- |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
225 |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
226 If you've got the stomach for it, list comprehensions can be nested. They are a |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
227 powerful tool but -- like all powerful tools -- they need to be used carefully, |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
228 if at all. |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
229 |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
230 Consider the following example of a 3x3 matrix held as a list containing three |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
231 lists, one list per row:: |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
232 |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
233 >>> mat = [ |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
234 ... [1, 2, 3], |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
235 ... [4, 5, 6], |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
236 ... [7, 8, 9], |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
237 ... ] |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
238 |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
239 Now, if you wanted to swap rows and columns, you could use a list |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
240 comprehension:: |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
241 |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
242 >>> print [[row[i] for row in mat] for i in [0, 1, 2]] |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
243 [[1, 4, 7], [2, 5, 8], [3, 6, 9]] |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
244 |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
245 Special care has to be taken for the *nested* list comprehension: |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
246 |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
247 To avoid apprehension when nesting list comprehensions, read from right to |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
248 left. |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
249 |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
250 A more verbose version of this snippet shows the flow explicitly:: |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
251 |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
252 for i in [0, 1, 2]: |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
253 for row in mat: |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
254 print row[i], |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
255 print |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
256 |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
257 In real world, you should prefer builtin functions to complex flow statements. |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
258 The :func:`zip` function would do a great job for this use case:: |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
259 |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
260 >>> zip(*mat) |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
261 [(1, 4, 7), (2, 5, 8), (3, 6, 9)] |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
262 |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
263 See :ref:`tut-unpacking-arguments` for details on the asterisk in this line. |
|
e70acbf26cda
Merged revisions 59488-59511 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43497
diff
changeset
|
264 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
265 .. _tut-del: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
266 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
267 The :keyword:`del` statement |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
268 ============================ |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
269 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
270 There is a way to remove an item from a list given its index instead of its |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
271 value: the :keyword:`del` statement. This differs from the :meth:`pop` method |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
272 which returns a value. The :keyword:`del` statement can also be used to remove |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
273 slices from a list or clear the entire list (which we did earlier by assignment |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
274 of an empty list to the slice). For example:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
275 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
276 >>> a = [-1, 1, 66.25, 333, 333, 1234.5] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
277 >>> del a[0] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
278 >>> a |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
279 [1, 66.25, 333, 333, 1234.5] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
280 >>> del a[2:4] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
281 >>> a |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
282 [1, 66.25, 1234.5] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
283 >>> del a[:] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
284 >>> a |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
285 [] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
286 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
287 :keyword:`del` can also be used to delete entire variables:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
288 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
289 >>> del a |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
290 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
291 Referencing the name ``a`` hereafter is an error (at least until another value |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
292 is assigned to it). We'll find other uses for :keyword:`del` later. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
293 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
294 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
295 |
|
44396
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
296 Tuples and Sequences |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
297 ==================== |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
298 |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
299 We saw that lists and strings have many common properties, such as indexing and |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
300 slicing operations. They are two examples of *sequence* data types (see |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
301 :ref:`typesseq`). Since Python is an evolving language, other sequence data |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
302 types may be added. There is also another standard sequence data type: the |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
303 *tuple*. |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
304 |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
305 A tuple consists of a number of values separated by commas, for instance:: |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
306 |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
307 >>> t = 12345, 54321, 'hello!' |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
308 >>> t[0] |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
309 12345 |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
310 >>> t |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
311 (12345, 54321, 'hello!') |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
312 >>> # Tuples may be nested: |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
313 ... u = t, (1, 2, 3, 4, 5) |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
314 >>> u |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
315 ((12345, 54321, 'hello!'), (1, 2, 3, 4, 5)) |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
316 |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
317 As you see, on output tuples are always enclosed in parentheses, so that nested |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
318 tuples are interpreted correctly; they may be input with or without surrounding |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
319 parentheses, although often parentheses are necessary anyway (if the tuple is |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
320 part of a larger expression). |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
321 |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
322 Tuples have many uses. For example: (x, y) coordinate pairs, employee records |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
323 from a database, etc. Tuples, like strings, are immutable: it is not possible |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
324 to assign to the individual items of a tuple (you can simulate much of the same |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
325 effect with slicing and concatenation, though). It is also possible to create |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
326 tuples which contain mutable objects, such as lists. |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
327 |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
328 A special problem is the construction of tuples containing 0 or 1 items: the |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
329 syntax has some extra quirks to accommodate these. Empty tuples are constructed |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
330 by an empty pair of parentheses; a tuple with one item is constructed by |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
331 following a value with a comma (it is not sufficient to enclose a single value |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
332 in parentheses). Ugly, but effective. For example:: |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
333 |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
334 >>> empty = () |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
335 >>> singleton = 'hello', # <-- note trailing comma |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
336 >>> len(empty) |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
337 0 |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
338 >>> len(singleton) |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
339 1 |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
340 >>> singleton |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
341 ('hello',) |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
342 |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
343 The statement ``t = 12345, 54321, 'hello!'`` is an example of *tuple packing*: |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
344 the values ``12345``, ``54321`` and ``'hello!'`` are packed together in a tuple. |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
345 The reverse operation is also possible:: |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
346 |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
347 >>> x, y, z = t |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
348 |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
349 This is called, appropriately enough, *sequence unpacking*. Sequence unpacking |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
350 requires the list of variables on the left to have the same number of elements |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
351 as the length of the sequence. Note that multiple assignment is really just a |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
352 combination of tuple packing and sequence unpacking! |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
353 |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
354 There is a small bit of asymmetry here: packing multiple values always creates |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
355 a tuple, and unpacking works for any sequence. |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
356 |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
357 .. XXX Add a bit on the difference between tuples and lists. |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
358 |
|
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44310
diff
changeset
|
359 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
360 .. _tut-sets: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
361 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
362 Sets |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
363 ==== |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
364 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
365 Python also includes a data type for *sets*. A set is an unordered collection |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
366 with no duplicate elements. Basic uses include membership testing and |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
367 eliminating duplicate entries. Set objects also support mathematical operations |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
368 like union, intersection, difference, and symmetric difference. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
369 |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
370 Curly braces or the :func:`set` function can be use to create sets. Note: |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
371 To create an empty set you have to use set(), not {}; the latter creates |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
372 an empty dictionary, a data structure that we discuss in the next section. |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
373 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
374 Here is a brief demonstration:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
375 |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
376 >>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'} |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
377 >>> print(basket) |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
378 {'orange', 'bananna', 'pear', 'apple'} |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
379 >>> fruit = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
380 >>> fruit = set(basket) # create a set without duplicates |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
381 >>> fruit |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
382 {'orange', 'pear', 'apple', 'banana'} |
|
45084
38604fa2a48f
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
Georg Brandl <georg@python.org>
parents:
44396
diff
changeset
|
383 >>> fruit = {'orange', 'apple'} # {} syntax is equivalent to [] for lists |
|
38604fa2a48f
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
Georg Brandl <georg@python.org>
parents:
44396
diff
changeset
|
384 >>> fruit |
|
38604fa2a48f
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
Georg Brandl <georg@python.org>
parents:
44396
diff
changeset
|
385 {'orange', 'apple'} |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
386 >>> 'orange' in fruit # fast membership testing |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
387 True |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
388 >>> 'crabgrass' in fruit |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
389 False |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
390 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
391 >>> # Demonstrate set operations on unique letters from two words |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
392 ... |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
393 >>> a = set('abracadabra') |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
394 >>> b = set('alacazam') |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
395 >>> a # unique letters in a |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
396 {'a', 'r', 'b', 'c', 'd'} |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
397 >>> a - b # letters in a but not in b |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
398 {'r', 'd', 'b'} |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
399 >>> a | b # letters in either a or b |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
400 {'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'} |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
401 >>> a & b # letters in both a and b |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
402 {'a', 'c'} |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
403 >>> a ^ b # letters in a or b but not both |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
404 {'r', 'd', 'b', 'm', 'z', 'l'} |
|
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
405 |
|
45084
38604fa2a48f
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
Georg Brandl <georg@python.org>
parents:
44396
diff
changeset
|
406 Like for lists, there is a set comprehension syntax:: |
|
38604fa2a48f
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
Georg Brandl <georg@python.org>
parents:
44396
diff
changeset
|
407 |
|
38604fa2a48f
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
Georg Brandl <georg@python.org>
parents:
44396
diff
changeset
|
408 >>> a = {x for x in 'abracadabra' if x not in 'abc'} |
|
38604fa2a48f
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
Georg Brandl <georg@python.org>
parents:
44396
diff
changeset
|
409 >>> a |
|
38604fa2a48f
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
Georg Brandl <georg@python.org>
parents:
44396
diff
changeset
|
410 {'r', 'd'} |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
411 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
412 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
413 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
414 .. _tut-dictionaries: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
415 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
416 Dictionaries |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
417 ============ |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
418 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
419 Another useful data type built into Python is the *dictionary* (see |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
420 :ref:`typesmapping`). Dictionaries are sometimes found in other languages as |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
421 "associative memories" or "associative arrays". Unlike sequences, which are |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
422 indexed by a range of numbers, dictionaries are indexed by *keys*, which can be |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
423 any immutable type; strings and numbers can always be keys. Tuples can be used |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
424 as keys if they contain only strings, numbers, or tuples; if a tuple contains |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
425 any mutable object either directly or indirectly, it cannot be used as a key. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
426 You can't use lists as keys, since lists can be modified in place using index |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
427 assignments, slice assignments, or methods like :meth:`append` and |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
428 :meth:`extend`. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
429 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
430 It is best to think of a dictionary as an unordered set of *key: value* pairs, |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
431 with the requirement that the keys are unique (within one dictionary). A pair of |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
432 braces creates an empty dictionary: ``{}``. Placing a comma-separated list of |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
433 key:value pairs within the braces adds initial key:value pairs to the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
434 dictionary; this is also the way dictionaries are written on output. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
435 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
436 The main operations on a dictionary are storing a value with some key and |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
437 extracting the value given the key. It is also possible to delete a key:value |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
438 pair with ``del``. If you store using a key that is already in use, the old |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
439 value associated with that key is forgotten. It is an error to extract a value |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
440 using a non-existent key. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
441 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
442 The :meth:`keys` method of a dictionary object returns a list of all the keys |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
443 used in the dictionary, in arbitrary order (if you want it sorted, just apply |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
444 the :meth:`sort` method to the list of keys). To check whether a single key is |
|
43303
9176a53b4b62
Remove yet more references to has_key() methods.
Collin Winter <collinw@gmail.com>
parents:
43201
diff
changeset
|
445 in the dictionary, use the :keyword:`in` keyword. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
446 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
447 Here is a small example using a dictionary:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
448 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
449 >>> tel = {'jack': 4098, 'sape': 4139} |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
450 >>> tel['guido'] = 4127 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
451 >>> tel |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
452 {'sape': 4139, 'guido': 4127, 'jack': 4098} |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
453 >>> tel['jack'] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
454 4098 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
455 >>> del tel['sape'] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
456 >>> tel['irv'] = 4127 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
457 >>> tel |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
458 {'guido': 4127, 'irv': 4127, 'jack': 4098} |
|
43117
7ea1367ba275
A few more fixes to the tutorial
Neal Norwitz <nnorwitz@gmail.com>
parents:
43115
diff
changeset
|
459 >>> list(tel.keys()) |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
460 ['guido', 'irv', 'jack'] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
461 >>> 'guido' in tel |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
462 True |
|
43117
7ea1367ba275
A few more fixes to the tutorial
Neal Norwitz <nnorwitz@gmail.com>
parents:
43115
diff
changeset
|
463 >>> 'jack' not in tel |
|
7ea1367ba275
A few more fixes to the tutorial
Neal Norwitz <nnorwitz@gmail.com>
parents:
43115
diff
changeset
|
464 False |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
465 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
466 The :func:`dict` constructor builds dictionaries directly from lists of |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
467 key-value pairs stored as tuples. When the pairs form a pattern, list |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
468 comprehensions can compactly specify the key-value list. :: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
469 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
470 >>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
471 {'sape': 4139, 'jack': 4098, 'guido': 4127} |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
472 |
|
45084
38604fa2a48f
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
Georg Brandl <georg@python.org>
parents:
44396
diff
changeset
|
473 In addition, dict comprehensions can be used to create dictionaries from |
|
38604fa2a48f
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
Georg Brandl <georg@python.org>
parents:
44396
diff
changeset
|
474 arbitrary key and value expressions:: |
|
38604fa2a48f
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
Georg Brandl <georg@python.org>
parents:
44396
diff
changeset
|
475 |
|
38604fa2a48f
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
Georg Brandl <georg@python.org>
parents:
44396
diff
changeset
|
476 >>> {x: x**2 for x in (2, 4, 6)} |
|
38604fa2a48f
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
Georg Brandl <georg@python.org>
parents:
44396
diff
changeset
|
477 {2: 4, 4: 16, 6: 36} |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
478 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
479 When the keys are simple strings, it is sometimes easier to specify pairs using |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
480 keyword arguments:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
481 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
482 >>> dict(sape=4139, guido=4127, jack=4098) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
483 {'sape': 4139, 'jack': 4098, 'guido': 4127} |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
484 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
485 |
|
45084
38604fa2a48f
Update docs w.r.t. PEP 3100 changes -- patch for GHOP by Dan Finnie.
Georg Brandl <georg@python.org>
parents:
44396
diff
changeset
|
486 .. XXX Find out the right way to do these DUBOIS |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
487 .. _tut-loopidioms: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
488 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
489 Looping Techniques |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
490 ================== |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
491 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
492 When looping through dictionaries, the key and corresponding value can be |
|
43117
7ea1367ba275
A few more fixes to the tutorial
Neal Norwitz <nnorwitz@gmail.com>
parents:
43115
diff
changeset
|
493 retrieved at the same time using the :meth:`items` method. :: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
494 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
495 >>> knights = {'gallahad': 'the pure', 'robin': 'the brave'} |
|
43117
7ea1367ba275
A few more fixes to the tutorial
Neal Norwitz <nnorwitz@gmail.com>
parents:
43115
diff
changeset
|
496 >>> for k, v in knights.items(): |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
497 ... print(k, v) |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
498 ... |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
499 gallahad the pure |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
500 robin the brave |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
501 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
502 When looping through a sequence, the position index and corresponding value can |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
503 be retrieved at the same time using the :func:`enumerate` function. :: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
504 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
505 >>> for i, v in enumerate(['tic', 'tac', 'toe']): |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
506 ... print(i, v) |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
507 ... |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
508 0 tic |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
509 1 tac |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
510 2 toe |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
511 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
512 To loop over two or more sequences at the same time, the entries can be paired |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
513 with the :func:`zip` function. :: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
514 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
515 >>> questions = ['name', 'quest', 'favorite color'] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
516 >>> answers = ['lancelot', 'the holy grail', 'blue'] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
517 >>> for q, a in zip(questions, answers): |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
518 ... print('What is your %s? It is %s.' % (q, a)) |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
519 ... |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
520 What is your name? It is lancelot. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
521 What is your quest? It is the holy grail. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
522 What is your favorite color? It is blue. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
523 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
524 To loop over a sequence in reverse, first specify the sequence in a forward |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
525 direction and then call the :func:`reversed` function. :: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
526 |
|
43201
4902671dd678
Tutorial formatting patch by Robin Stocker.
Georg Brandl <georg@python.org>
parents:
43117
diff
changeset
|
527 >>> for i in reversed(range(1, 10, 2)): |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
528 ... print(i) |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
529 ... |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
530 9 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
531 7 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
532 5 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
533 3 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
534 1 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
535 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
536 To loop over a sequence in sorted order, use the :func:`sorted` function which |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
537 returns a new sorted list while leaving the source unaltered. :: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
538 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
539 >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
540 >>> for f in sorted(set(basket)): |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
541 ... print(f) |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
542 ... |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
543 apple |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
544 banana |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
545 orange |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
546 pear |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
547 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
548 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
549 .. _tut-conditions: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
550 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
551 More on Conditions |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
552 ================== |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
553 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
554 The conditions used in ``while`` and ``if`` statements can contain any |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
555 operators, not just comparisons. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
556 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
557 The comparison operators ``in`` and ``not in`` check whether a value occurs |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
558 (does not occur) in a sequence. The operators ``is`` and ``is not`` compare |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
559 whether two objects are really the same object; this only matters for mutable |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
560 objects like lists. All comparison operators have the same priority, which is |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
561 lower than that of all numerical operators. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
562 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
563 Comparisons can be chained. For example, ``a < b == c`` tests whether ``a`` is |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
564 less than ``b`` and moreover ``b`` equals ``c``. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
565 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
566 Comparisons may be combined using the Boolean operators ``and`` and ``or``, and |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
567 the outcome of a comparison (or of any other Boolean expression) may be negated |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
568 with ``not``. These have lower priorities than comparison operators; between |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
569 them, ``not`` has the highest priority and ``or`` the lowest, so that ``A and |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
570 not B or C`` is equivalent to ``(A and (not B)) or C``. As always, parentheses |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
571 can be used to express the desired composition. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
572 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
573 The Boolean operators ``and`` and ``or`` are so-called *short-circuit* |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
574 operators: their arguments are evaluated from left to right, and evaluation |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
575 stops as soon as the outcome is determined. For example, if ``A`` and ``C`` are |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
576 true but ``B`` is false, ``A and B and C`` does not evaluate the expression |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
577 ``C``. When used as a general value and not as a Boolean, the return value of a |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
578 short-circuit operator is the last evaluated argument. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
579 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
580 It is possible to assign the result of a comparison or other Boolean expression |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
581 to a variable. For example, :: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
582 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
583 >>> string1, string2, string3 = '', 'Trondheim', 'Hammer Dance' |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
584 >>> non_null = string1 or string2 or string3 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
585 >>> non_null |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
586 'Trondheim' |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
587 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
588 Note that in Python, unlike C, assignment cannot occur inside expressions. C |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
589 programmers may grumble about this, but it avoids a common class of problems |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
590 encountered in C programs: typing ``=`` in an expression when ``==`` was |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
591 intended. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
592 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
593 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
594 .. _tut-comparing: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
595 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
596 Comparing Sequences and Other Types |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
597 =================================== |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
598 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
599 Sequence objects may be compared to other objects with the same sequence type. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
600 The comparison uses *lexicographical* ordering: first the first two items are |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
601 compared, and if they differ this determines the outcome of the comparison; if |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
602 they are equal, the next two items are compared, and so on, until either |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
603 sequence is exhausted. If two items to be compared are themselves sequences of |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
604 the same type, the lexicographical comparison is carried out recursively. If |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
605 all items of two sequences compare equal, the sequences are considered equal. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
606 If one sequence is an initial sub-sequence of the other, the shorter sequence is |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
607 the smaller (lesser) one. Lexicographical ordering for strings uses the ASCII |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
608 ordering for individual characters. Some examples of comparisons between |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
609 sequences of the same type:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
610 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
611 (1, 2, 3) < (1, 2, 4) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
612 [1, 2, 3] < [1, 2, 4] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
613 'ABC' < 'C' < 'Pascal' < 'Python' |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
614 (1, 2, 3, 4) < (1, 2, 4) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
615 (1, 2) < (1, 2, -1) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
616 (1, 2, 3) == (1.0, 2.0, 3.0) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
617 (1, 2, ('aa', 'ab')) < (1, 2, ('abc', 'a'), 4) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
618 |
|
43497
6614d42d7f0a
#1228: new comparison behavior.
Georg Brandl <georg@python.org>
parents:
43303
diff
changeset
|
619 Note that comparing objects of different types with ``<`` or ``>`` is legal |
|
6614d42d7f0a
#1228: new comparison behavior.
Georg Brandl <georg@python.org>
parents:
43303
diff
changeset
|
620 provided that the objects have appropriate comparison methods. For example, |
|
6614d42d7f0a
#1228: new comparison behavior.
Georg Brandl <georg@python.org>
parents:
43303
diff
changeset
|
621 mixed numeric types are compared according to their numeric value, so 0 equals |
|
6614d42d7f0a
#1228: new comparison behavior.
Georg Brandl <georg@python.org>
parents:
43303
diff
changeset
|
622 0.0, etc. Otherwise, rather than providing an arbitrary ordering, the |
|
6614d42d7f0a
#1228: new comparison behavior.
Georg Brandl <georg@python.org>
parents:
43303
diff
changeset
|
623 interpreter will raise a :exc:`TypeError` exception. |
