-
Notifications
You must be signed in to change notification settings - Fork 0
/
Python_ List Comprehensions.html
214 lines (188 loc) · 9.37 KB
/
Python_ List Comprehensions.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0059)http://www.secnetix.de/olli/Python/list_comprehensions.hawk -->
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css" media="screen"><!--
body {
background: #ffffff ;
color: #000000
}
A:link { color: #0000ff }
A:active { color: #00a0a0 }
A:visited { color: #a000a0 }
img.borderless {
border: none ;
border-color: #ffffff ;
border-width: 0px
}
th { text-decoration: underline }
div.vspace { padding-top: 3ex }
span.hspace { padding-left: 3em }
p { width: 75ex ; text-align: justify }
.just { width: 75ex ; text-align: justify }
--></style>
<title>Python: List Comprehensions</title>
<link rel="stylesheet" type="text/css" href="chrome-extension://pkehgijcmpdhfbdbbnkijodmdjhbjlgp/skin/socialwidgets.css"></head>
<body bgcolor="#ffffff" text="#000000" link="0000ff" vlink="a000a0" alink="00a0a0">
<div style="width: 75ex ; text-align: justify">
<h1>Python: List Comprehensions</h1>
<table border="1" cellspacing="0" cellpadding="6"><tbody><tr>
<td class="just">
<font size="-1">
Note: Lines beginning with "<code>>>></code>"
and "<code>...</code>" indicate input to Python (these
are the default prompts of the interactive interpreter).
Everything else is output from Python.
</font>
</td>
</tr></tbody></table>
<p>
Python supports a concept called "list comprehensions".
It can be used to construct lists in a very natural,
easy way, like a mathematician is used to do.
</p>
<p>
The following are common ways to describe lists (or
sets, or tuples, or vectors) in mathematics.
</p>
</div>
<table border="0" cellspacing="0" cellpadding="12">
<tbody><tr><td width="50"></td>
<td bgcolor="#d0d0ff">
<code>S = {x² : x in {0 ... 9}}</code><br>
<code>V = (1, 2, 4, 8, ..., 2¹²)</code><br>
<code>M = {x | x in S and x even}</code><br>
</td></tr></tbody></table>
<div style="width: 75ex ; text-align: justify">
<p>
You probably know things like the above from mathematics
lessons at school. In Python, you can write these
expression almost exactly like a mathematician would
do, without having to remember any special cryptic
syntax.
</p>
<p>
This is how you do the above in Python:
</p>
</div>
<table border="0" cellspacing="0" cellpadding="12">
<tbody><tr><td width="50"></td>
<td bgcolor="#d0d0ff">
<font color="#0000ff"><code>>>> </code></font><code>S = [x**2 for x in range(10)]</code><br>
<font color="#0000ff"><code>>>> </code></font><code>V = [2**i for i in range(13)]</code><br>
<font color="#0000ff"><code>>>> </code></font><code>M = [x for x in S if x % 2 == 0]</code><br>
<font color="#0000ff"><code>>>> </code></font><code></code><br>
<font color="#0000ff"><code>>>> </code></font><code>print S; print V; print M</code><br>
<table cellpadding="2" cellspacing="0" bgcolor="#d0f0ff" width="100%"><tbody><tr><td>
<code>[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</code><br>
<code>[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096]</code><br>
<code>[0, 4, 16, 36, 64]</code><br>
</td></tr></tbody></table>
</td></tr></tbody></table>
<div style="width: 75ex ; text-align: justify">
<p>
I'm sure you want to see a more complicated example. :-)
The following is yet another way to compute prime numbers.
The interesting thing is that we first build a list of
non-prime numbers, using a single list comprehension,
then use another list comprehension to get the "inverse"
of the list, which are prime numbers.
</p>
</div>
<table border="0" cellspacing="0" cellpadding="12">
<tbody><tr><td width="50"></td>
<td bgcolor="#d0d0ff">
<font color="#0000ff"><code>>>> </code></font><code>noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]</code><br>
<font color="#0000ff"><code>>>> </code></font><code>primes = [x for x in range(2, 50) if x not in noprimes]</code><br>
<font color="#0000ff"><code>>>> </code></font><code>print primes</code><br>
<table cellpadding="2" cellspacing="0" bgcolor="#d0f0ff" width="100%"><tbody><tr><td>
<code>[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]</code><br>
</td></tr></tbody></table>
</td></tr></tbody></table>
<div style="width: 75ex ; text-align: justify">
<p>
NB: You can nest list comprehensions inside of each other,
so you could write the above example with a single
statement (without the need for the temporary variable
"<code>noprimes</code>"). However, such lines tend to get
long and less readable, so this is not recommended.
</p>
<p>
Of course, list comprehensions don't only work for numbers.
Lists can contain <em>any</em> type of elements, including
strings, nested lists and functions. You can even
mix different types within a list.
</p>
<p>
The following works on a list of strings and produces a
list of lists. Each of the sublists contains two strings
and an integer.
</p>
</div>
<table border="0" cellspacing="0" cellpadding="12">
<tbody><tr><td width="50"></td>
<td bgcolor="#d0d0ff">
<font color="#0000ff"><code>>>> </code></font><code>words = 'The quick brown fox jumps over the lazy dog'.split()</code><br>
<font color="#0000ff"><code>>>> </code></font><code>print words</code><br>
<table cellpadding="2" cellspacing="0" bgcolor="#d0f0ff" width="100%"><tbody><tr><td>
<code>['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']</code><br>
</td></tr></tbody></table>
<font color="#0000ff"><code>>>> </code></font><code></code><br>
<font color="#0000ff"><code>>>> </code></font><code>stuff = [[w.upper(), w.lower(), len(w)] for w in words]</code><br>
<font color="#0000ff"><code>>>> </code></font><code>for i in stuff:</code><br>
<font color="#0000ff"><code>... </code></font><code> print i</code><br>
<font color="#0000ff"><code>... </code></font><code></code><br>
<table cellpadding="2" cellspacing="0" bgcolor="#d0f0ff" width="100%"><tbody><tr><td>
<code>['THE', 'the', 3]</code><br>
<code>['QUICK', 'quick', 5]</code><br>
<code>['BROWN', 'brown', 5]</code><br>
<code>['FOX', 'fox', 3]</code><br>
<code>['JUMPS', 'jumps', 5]</code><br>
<code>['OVER', 'over', 4]</code><br>
<code>['THE', 'the', 3]</code><br>
<code>['LAZY', 'lazy', 4]</code><br>
<code>['DOG', 'dog', 3]</code><br>
</td></tr></tbody></table>
<font color="#0000ff"><code>>>> </code></font><code></code><br>
<font color="#0000ff"><code>>>> </code></font><code>stuff = map(lambda w: [w.upper(), w.lower(), len(w)], words)</code><br>
<font color="#0000ff"><code>>>> </code></font><code>for i in stuff:</code><br>
<font color="#0000ff"><code>... </code></font><code> print i</code><br>
<font color="#0000ff"><code>... </code></font><code></code><br>
<table cellpadding="2" cellspacing="0" bgcolor="#d0f0ff" width="100%"><tbody><tr><td>
<code>['THE', 'the', 3]</code><br>
<code>['QUICK', 'quick', 5]</code><br>
<code>['BROWN', 'brown', 5]</code><br>
<code>['FOX', 'fox', 3]</code><br>
<code>['JUMPS', 'jumps', 5]</code><br>
<code>['OVER', 'over', 4]</code><br>
<code>['THE', 'the', 3]</code><br>
<code>['LAZY', 'lazy', 4]</code><br>
<code>['DOG', 'dog', 3]</code><br>
</td></tr></tbody></table>
</td></tr></tbody></table>
<div style="width: 75ex ; text-align: justify">
<p>
The above example also demonstrates that you can do exactly
the same thing with <code>map()</code> and a lambda function.
However, there are cases when you cannot
use <code>map()</code> and have to use a list comprehension
instead, or vice versa. When you can use both, then it
is often preferable to use a list comprehension,
because this is more efficient and easier to read,
most of the time.
</p>
<p>
You cannot use list comprehensions when the construction
rule is too complicated to be expressed with "for" and "if"
statements, or if the construction rule can change dynamically
at runtime. In this case, you better
use <code>map()</code> and / or <code>filter()</code> with an
appropriate function.
Of course, you can combine that with list comprehensions.
</p>
</div>
<hr>
<p>
<a href="http://validator.w3.org/check?uri=http://www.secnetix.de/olli/Python/list_comprehensions.hawk"><img src="./Python_ List Comprehensions_files/vh401.gif" alt="[HTML 4.01]" border="0" class="borderless" width="88" height="31"></a>
</p>
</body></html>