|
1 | 1 | {
|
2 | 2 | "metadata": {
|
3 | 3 | "name": "",
|
4 |
| - "signature": "sha256:20f6b02042c480fae323fa6c98332c749bfe323d54bb24066930544c93c73a33" |
| 4 | + "signature": "sha256:826b0e4b3d39f844b868b8370fcaac110d06dbe6afb167b149a60d16aea5592e" |
5 | 5 | },
|
6 | 6 | "nbformat": 3,
|
7 | 7 | "nbformat_minor": 0,
|
|
23 | 23 | "\n",
|
24 | 24 | "Now we're going to learn about list comprehensions.\n",
|
25 | 25 | "\n",
|
26 |
| - "A list comprehension is kind of like an inside-out for loop. It makes it easy to do operations on elements in a list and return a new list." |
| 26 | + "A list comprehension is kind of like a reverse for-loop. It makes it easy to do operations on elements in a list and return a new list." |
27 | 27 | ]
|
28 | 28 | },
|
29 | 29 | {
|
|
83 | 83 | "cell_type": "markdown",
|
84 | 84 | "metadata": {},
|
85 | 85 | "source": [
|
86 |
| - "Now let's do the same thing with a list comprehension:" |
| 86 | + "Now let's do the same thing by using a list comprehension instead:" |
87 | 87 | ]
|
88 | 88 | },
|
89 | 89 | {
|
|
110 | 110 | "cell_type": "markdown",
|
111 | 111 | "metadata": {},
|
112 | 112 | "source": [
|
113 |
| - "Let's revisit a problem we've already solved:\n", |
| 113 | + "Note how our list comprehension is written within the brackets: The for-loop statement is written at the end whereas the action inside of the for-loop is written first: n * n" |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "markdown", |
| 118 | + "metadata": {}, |
| 119 | + "source": [ |
| 120 | + "Let's revisit a problem we've already solved in Danny's lecture on list:\n", |
114 | 121 | "\n",
|
115 | 122 | "Pick every name from a list that begins with a vowel.\n",
|
116 | 123 | "\n",
|
|
121 | 128 | "cell_type": "code",
|
122 | 129 | "collapsed": false,
|
123 | 130 | "input": [
|
124 |
| - "names = [\"Alice\", \"Bob\", \"Cassie\", \"Diane\", \"Ellen\"]" |
| 131 | + "names = [\"Danny\", \"Audrey\", \"Rise\", \"Alain\"]" |
125 | 132 | ],
|
126 | 133 | "language": "python",
|
127 | 134 | "metadata": {},
|
|
147 | 154 | "metadata": {},
|
148 | 155 | "outputs": []
|
149 | 156 | },
|
| 157 | + { |
| 158 | + "cell_type": "code", |
| 159 | + "collapsed": false, |
| 160 | + "input": [ |
| 161 | + "vowel_names" |
| 162 | + ], |
| 163 | + "language": "python", |
| 164 | + "metadata": {}, |
| 165 | + "outputs": [] |
| 166 | + }, |
150 | 167 | {
|
151 | 168 | "cell_type": "markdown",
|
152 | 169 | "metadata": {},
|
|
180 | 197 | "source": [
|
181 | 198 | "## Dictionaries\n",
|
182 | 199 | "\n",
|
183 |
| - "Unlike lists, dictionaries are indexed by keys. Dictionaries can be used to represent unordered key-value pairs. Keys are used for lookup and their values are returned.\n", |
| 200 | + "Unlike lists, dictionaries are indexed by keys. Dictionaries can be used to represent unordered key-value pairs. Keys are unique and are used for looking up values assigned to them.\n", |
184 | 201 | "\n",
|
185 |
| - "Let's make an Spanish to English translator. We'll ignore grammar and just translate word-by-word for now." |
| 202 | + "Let's make a Spanish to English translator. We'll ignore grammar and just translate word-by-word for now." |
186 | 203 | ]
|
187 | 204 | },
|
188 | 205 | {
|
|
269 | 286 | "cell_type": "markdown",
|
270 | 287 | "metadata": {},
|
271 | 288 | "source": [
|
272 |
| - "Let's loop over each word in the list and translate each one." |
| 289 | + "Let's translate each word and save them in the list called translated_words. We'll generate this list by using a list comprehension." |
273 | 290 | ]
|
274 | 291 | },
|
275 | 292 | {
|
276 | 293 | "cell_type": "code",
|
277 | 294 | "collapsed": false,
|
278 | 295 | "input": [
|
279 |
| - "for spanish_word in sentence_words:\n", |
280 |
| - " print(words[spanish_word])" |
| 296 | + "translated_words = [print(words[spanish_word]) for spanish_word in sentence_words]" |
281 | 297 | ],
|
282 | 298 | "language": "python",
|
283 | 299 | "metadata": {},
|
284 | 300 | "outputs": []
|
285 | 301 | },
|
286 |
| - { |
287 |
| - "cell_type": "markdown", |
288 |
| - "metadata": {}, |
289 |
| - "source": [ |
290 |
| - "Now let's make a list from the translated words." |
291 |
| - ] |
292 |
| - }, |
293 | 302 | {
|
294 | 303 | "cell_type": "code",
|
295 | 304 | "collapsed": false,
|
296 | 305 | "input": [
|
297 |
| - "translated_words = []\n", |
298 |
| - "for spanish_word in sentence_words:\n", |
299 |
| - " translated_words.append(words[spanish_word])\n", |
300 |
| - "\n", |
301 | 306 | "translated_words"
|
302 | 307 | ],
|
303 | 308 | "language": "python",
|
|
391 | 396 | "cell_type": "markdown",
|
392 | 397 | "metadata": {},
|
393 | 398 | "source": [
|
394 |
| - "**Extra Credit:** turn the translate function into a single list comprehension." |
| 399 | + "**Extra Credit:** Try using a list comprehension in the translate function." |
395 | 400 | ]
|
396 | 401 | },
|
397 | 402 | {
|
|
549 | 554 | "language": "python",
|
550 | 555 | "metadata": {},
|
551 | 556 | "outputs": []
|
| 557 | + }, |
| 558 | + { |
| 559 | + "cell_type": "code", |
| 560 | + "collapsed": false, |
| 561 | + "input": [], |
| 562 | + "language": "python", |
| 563 | + "metadata": {}, |
| 564 | + "outputs": [] |
552 | 565 | }
|
553 | 566 | ],
|
554 | 567 | "metadata": {}
|
|
0 commit comments