|
1 | 1 | {
|
2 | 2 | "cells": [
|
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": { |
| 6 | + "collapsed": true |
| 7 | + }, |
| 8 | + "source": [ |
| 9 | + "#Comprehensions\n", |
| 10 | + "\n", |
| 11 | + "In addition to sequence operations and list methods, Python includes a more advanced operation called a list comprehension.\n", |
| 12 | + "\n", |
| 13 | + "List comprehensions allow us to build out lists using a different notation. You can think of it as essentially a one line for loop built inside of brackets. For a simple example:\n", |
| 14 | + "##Example 1" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": 1, |
| 20 | + "metadata": { |
| 21 | + "collapsed": true |
| 22 | + }, |
| 23 | + "outputs": [], |
| 24 | + "source": [ |
| 25 | + "# Grab every letter in string\n", |
| 26 | + "lst = [x for x in 'word']" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "code", |
| 31 | + "execution_count": 2, |
| 32 | + "metadata": { |
| 33 | + "collapsed": false |
| 34 | + }, |
| 35 | + "outputs": [ |
| 36 | + { |
| 37 | + "data": { |
| 38 | + "text/plain": [ |
| 39 | + "['w', 'o', 'r', 'd']" |
| 40 | + ] |
| 41 | + }, |
| 42 | + "execution_count": 2, |
| 43 | + "metadata": {}, |
| 44 | + "output_type": "execute_result" |
| 45 | + } |
| 46 | + ], |
| 47 | + "source": [ |
| 48 | + "# Check\n", |
| 49 | + "lst" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "markdown", |
| 54 | + "metadata": {}, |
| 55 | + "source": [ |
| 56 | + "This is the basic idea of a list comprehension. If you're familiar with mathematical notation this format should feel familiar for example: x^2 : x in { 0,1,2...10} \n", |
| 57 | + "\n", |
| 58 | + "Lets see a few more example of list comprehensions in Python:\n", |
| 59 | + "##Example 2" |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "code", |
| 64 | + "execution_count": 3, |
| 65 | + "metadata": { |
| 66 | + "collapsed": true |
| 67 | + }, |
| 68 | + "outputs": [], |
| 69 | + "source": [ |
| 70 | + "# Square numbers in range and turn into list\n", |
| 71 | + "lst = [x*2 for x in range(0,11)]" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "code", |
| 76 | + "execution_count": 4, |
| 77 | + "metadata": { |
| 78 | + "collapsed": false |
| 79 | + }, |
| 80 | + "outputs": [ |
| 81 | + { |
| 82 | + "data": { |
| 83 | + "text/plain": [ |
| 84 | + "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]" |
| 85 | + ] |
| 86 | + }, |
| 87 | + "execution_count": 4, |
| 88 | + "metadata": {}, |
| 89 | + "output_type": "execute_result" |
| 90 | + } |
| 91 | + ], |
| 92 | + "source": [ |
| 93 | + "lst" |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "markdown", |
| 98 | + "metadata": {}, |
| 99 | + "source": [ |
| 100 | + "##Example 3\n", |
| 101 | + "Lets see how to add in if statements:" |
| 102 | + ] |
| 103 | + }, |
3 | 104 | {
|
4 | 105 | "cell_type": "code",
|
5 |
| - "execution_count": null, |
| 106 | + "execution_count": 5, |
6 | 107 | "metadata": {
|
7 | 108 | "collapsed": true
|
8 | 109 | },
|
9 | 110 | "outputs": [],
|
10 |
| - "source": [] |
| 111 | + "source": [ |
| 112 | + "# Check for even numbers in a range\n", |
| 113 | + "lst = [x for x in range(11) if x % 2 == 0]" |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "code", |
| 118 | + "execution_count": 6, |
| 119 | + "metadata": { |
| 120 | + "collapsed": false |
| 121 | + }, |
| 122 | + "outputs": [ |
| 123 | + { |
| 124 | + "data": { |
| 125 | + "text/plain": [ |
| 126 | + "[0, 2, 4, 6, 8, 10]" |
| 127 | + ] |
| 128 | + }, |
| 129 | + "execution_count": 6, |
| 130 | + "metadata": {}, |
| 131 | + "output_type": "execute_result" |
| 132 | + } |
| 133 | + ], |
| 134 | + "source": [ |
| 135 | + "lst" |
| 136 | + ] |
| 137 | + }, |
| 138 | + { |
| 139 | + "cell_type": "markdown", |
| 140 | + "metadata": {}, |
| 141 | + "source": [ |
| 142 | + "##Example 4\n", |
| 143 | + "Can also do more complicated arithmetic:" |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + "cell_type": "code", |
| 148 | + "execution_count": 7, |
| 149 | + "metadata": { |
| 150 | + "collapsed": false |
| 151 | + }, |
| 152 | + "outputs": [ |
| 153 | + { |
| 154 | + "data": { |
| 155 | + "text/plain": [ |
| 156 | + "[32.0, 50.0, 68.18, 94.1]" |
| 157 | + ] |
| 158 | + }, |
| 159 | + "execution_count": 7, |
| 160 | + "metadata": {}, |
| 161 | + "output_type": "execute_result" |
| 162 | + } |
| 163 | + ], |
| 164 | + "source": [ |
| 165 | + "# Convert Celsius to Fahrenheit\n", |
| 166 | + "celsius = [0,10,20.1,34.5]\n", |
| 167 | + "\n", |
| 168 | + "fahrenheit = [ ((float(9)/5)*temp + 32) for temp in celsius ]\n", |
| 169 | + "\n", |
| 170 | + "fahrenheit" |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "markdown", |
| 175 | + "metadata": {}, |
| 176 | + "source": [ |
| 177 | + "##Example 5\n", |
| 178 | + "We can also perform nested list comprehensions, for example:" |
| 179 | + ] |
| 180 | + }, |
| 181 | + { |
| 182 | + "cell_type": "code", |
| 183 | + "execution_count": 8, |
| 184 | + "metadata": { |
| 185 | + "collapsed": false |
| 186 | + }, |
| 187 | + "outputs": [ |
| 188 | + { |
| 189 | + "data": { |
| 190 | + "text/plain": [ |
| 191 | + "[0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]" |
| 192 | + ] |
| 193 | + }, |
| 194 | + "execution_count": 8, |
| 195 | + "metadata": {}, |
| 196 | + "output_type": "execute_result" |
| 197 | + } |
| 198 | + ], |
| 199 | + "source": [ |
| 200 | + "lst = [ x**2 for x in [x**2 for x in range(11)]]\n", |
| 201 | + "lst" |
| 202 | + ] |
| 203 | + }, |
| 204 | + { |
| 205 | + "cell_type": "markdown", |
| 206 | + "metadata": {}, |
| 207 | + "source": [ |
| 208 | + "Later on in the course we will learn about generator comprehensions. After this lecture you should feel comfortable reading and writing basic list comprehensions." |
| 209 | + ] |
11 | 210 | }
|
12 | 211 | ],
|
13 | 212 | "metadata": {
|
|
0 commit comments