|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# map()\n", |
| 8 | + "\n", |
| 9 | + "map() is a built-in Python function that takes in two or more arguments: a function and one or more iterables, in the form:\n", |
| 10 | + "\n", |
| 11 | + " map(function, iterable, ...)\n", |
| 12 | + " \n", |
| 13 | + "map() returns an *iterator* - that is, map() returns a special object that yields one result at a time as needed. We will learn more about iterators and generators in a future lecture. For now, since our examples are so small, we will cast map() as a list to see the results immediately.\n", |
| 14 | + "\n", |
| 15 | + "When we went over list comprehensions we created a small expression to convert Celsius to Fahrenheit. Let's do the same here but use map:" |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": 1, |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [], |
| 23 | + "source": [ |
| 24 | + "def fahrenheit(celsius):\n", |
| 25 | + " return (9/5)*celsius + 32\n", |
| 26 | + " \n", |
| 27 | + "temps = [0, 22.5, 40, 100]" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "markdown", |
| 32 | + "metadata": {}, |
| 33 | + "source": [ |
| 34 | + "Now let's see map() in action:" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": 2, |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [ |
| 42 | + { |
| 43 | + "data": { |
| 44 | + "text/plain": [ |
| 45 | + "[32.0, 72.5, 104.0, 212.0]" |
| 46 | + ] |
| 47 | + }, |
| 48 | + "execution_count": 2, |
| 49 | + "metadata": {}, |
| 50 | + "output_type": "execute_result" |
| 51 | + } |
| 52 | + ], |
| 53 | + "source": [ |
| 54 | + "F_temps = map(fahrenheit, temps)\n", |
| 55 | + "\n", |
| 56 | + "#Show\n", |
| 57 | + "list(F_temps)" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "markdown", |
| 62 | + "metadata": {}, |
| 63 | + "source": [ |
| 64 | + "In the example above, map() applies the fahrenheit function to every item in temps. However, we don't have to define our functions beforehand; we can use a lambda expression instead:" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "code", |
| 69 | + "execution_count": 3, |
| 70 | + "metadata": {}, |
| 71 | + "outputs": [ |
| 72 | + { |
| 73 | + "data": { |
| 74 | + "text/plain": [ |
| 75 | + "[32.0, 72.5, 104.0, 212.0]" |
| 76 | + ] |
| 77 | + }, |
| 78 | + "execution_count": 3, |
| 79 | + "metadata": {}, |
| 80 | + "output_type": "execute_result" |
| 81 | + } |
| 82 | + ], |
| 83 | + "source": [ |
| 84 | + "list(map(lambda x: (9/5)*x + 32, temps))" |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "markdown", |
| 89 | + "metadata": {}, |
| 90 | + "source": [ |
| 91 | + "Great! We got the same result! Using map with lambda expressions is much more common since the entire purpose of map() is to save effort on having to create manual for loops." |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "markdown", |
| 96 | + "metadata": {}, |
| 97 | + "source": [ |
| 98 | + "### map() with multiple iterables\n", |
| 99 | + "map() can accept more than one iterable. The iterables should be the same length - in the event that they are not, map() will stop as soon as the shortest iterable is exhausted.\n", |
| 100 | + "\n", |
| 101 | + "\n", |
| 102 | + "For instance, if our function is trying to add two values **x** and **y**, we can pass a list of **x** values and another list of **y** values to map(). The function (or lambda) will be fed the 0th index from each list, and then the 1st index, and so on until the n-th index is reached.\n", |
| 103 | + "\n", |
| 104 | + "Let's see this in action with two and then three lists:" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "code", |
| 109 | + "execution_count": 4, |
| 110 | + "metadata": {}, |
| 111 | + "outputs": [ |
| 112 | + { |
| 113 | + "data": { |
| 114 | + "text/plain": [ |
| 115 | + "[6, 8, 10, 12]" |
| 116 | + ] |
| 117 | + }, |
| 118 | + "execution_count": 4, |
| 119 | + "metadata": {}, |
| 120 | + "output_type": "execute_result" |
| 121 | + } |
| 122 | + ], |
| 123 | + "source": [ |
| 124 | + "a = [1,2,3,4]\n", |
| 125 | + "b = [5,6,7,8]\n", |
| 126 | + "c = [9,10,11,12]\n", |
| 127 | + "\n", |
| 128 | + "list(map(lambda x,y:x+y,a,b))" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "code", |
| 133 | + "execution_count": 5, |
| 134 | + "metadata": {}, |
| 135 | + "outputs": [ |
| 136 | + { |
| 137 | + "data": { |
| 138 | + "text/plain": [ |
| 139 | + "[15, 18, 21, 24]" |
| 140 | + ] |
| 141 | + }, |
| 142 | + "execution_count": 5, |
| 143 | + "metadata": {}, |
| 144 | + "output_type": "execute_result" |
| 145 | + } |
| 146 | + ], |
| 147 | + "source": [ |
| 148 | + "# Now all three lists\n", |
| 149 | + "list(map(lambda x,y,z:x+y+z,a,b,c))" |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + "cell_type": "markdown", |
| 154 | + "metadata": {}, |
| 155 | + "source": [ |
| 156 | + "We can see in the example above that the parameter **x** gets its values from the list **a**, while **y** gets its values from **b** and **z** from list **c**. Go ahead and play with your own example to make sure you fully understand mapping to more than one iterable.\n", |
| 157 | + "\n", |
| 158 | + "Great job! You should now have a basic understanding of the map() function." |
| 159 | + ] |
| 160 | + } |
| 161 | + ], |
| 162 | + "metadata": { |
| 163 | + "kernelspec": { |
| 164 | + "display_name": "Python 3", |
| 165 | + "language": "python", |
| 166 | + "name": "python3" |
| 167 | + }, |
| 168 | + "language_info": { |
| 169 | + "codemirror_mode": { |
| 170 | + "name": "ipython", |
| 171 | + "version": 3 |
| 172 | + }, |
| 173 | + "file_extension": ".py", |
| 174 | + "mimetype": "text/x-python", |
| 175 | + "name": "python", |
| 176 | + "nbconvert_exporter": "python", |
| 177 | + "pygments_lexer": "ipython3", |
| 178 | + "version": "3.6.2" |
| 179 | + } |
| 180 | + }, |
| 181 | + "nbformat": 4, |
| 182 | + "nbformat_minor": 1 |
| 183 | +} |
0 commit comments