Skip to content

Commit 94ff6b6

Browse files
committed
fix f
1 parent 24a30f5 commit 94ff6b6

File tree

2 files changed

+225
-7
lines changed

2 files changed

+225
-7
lines changed
+218
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# map()\n",
8+
"\n",
9+
"map() is a function that takes in two arguments: a function and a sequence iterable. In the form:\n",
10+
" map(function, sequence)\n",
11+
" \n",
12+
"The first argument is the name of a function and the second a sequence (e.g. a list). map() applies the function to all the elements of the sequence. It returns a new list with the elements changed by function.\n",
13+
"\n",
14+
"When we went over list comprehension we created a small expression to convert Fahrenheit to Celsius. Let's do the same here but use map.\n",
15+
"\n",
16+
"We'll start with two functions:"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 4,
22+
"metadata": {
23+
"collapsed": true
24+
},
25+
"outputs": [],
26+
"source": [
27+
"def fahrenheit(T):\n",
28+
" return ((float(9)/5)*T + 32)\n",
29+
"def celsius(T):\n",
30+
" return (float(5)/9)*(T-32)\n",
31+
" \n",
32+
"temp = [0, 22.5, 40,100]"
33+
]
34+
},
35+
{
36+
"cell_type": "markdown",
37+
"metadata": {},
38+
"source": [
39+
"Now lets see map() in action:"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 7,
45+
"metadata": {
46+
"collapsed": false
47+
},
48+
"outputs": [
49+
{
50+
"data": {
51+
"text/plain": [
52+
"[32.0, 72.5, 104.0, 212.0]"
53+
]
54+
},
55+
"execution_count": 7,
56+
"metadata": {},
57+
"output_type": "execute_result"
58+
}
59+
],
60+
"source": [
61+
"F_temps = map(fahrenheit, temp)\n",
62+
"\n",
63+
"#Show\n",
64+
"F_temps"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 8,
70+
"metadata": {
71+
"collapsed": false
72+
},
73+
"outputs": [
74+
{
75+
"data": {
76+
"text/plain": [
77+
"[0.0, 22.5, 40.0, 100.0]"
78+
]
79+
},
80+
"execution_count": 8,
81+
"metadata": {},
82+
"output_type": "execute_result"
83+
}
84+
],
85+
"source": [
86+
"# Convert back\n",
87+
"map(celsius, F_temps)"
88+
]
89+
},
90+
{
91+
"cell_type": "markdown",
92+
"metadata": {},
93+
"source": [
94+
"In the example above we haven't used a lambda expression. By using lambda, we wouldn't have had to define and name the functions fahrenheit() and celsius()."
95+
]
96+
},
97+
{
98+
"cell_type": "code",
99+
"execution_count": 10,
100+
"metadata": {
101+
"collapsed": false
102+
},
103+
"outputs": [
104+
{
105+
"data": {
106+
"text/plain": [
107+
"[0.0, 22.5, 40.0, 100.0]"
108+
]
109+
},
110+
"execution_count": 10,
111+
"metadata": {},
112+
"output_type": "execute_result"
113+
}
114+
],
115+
"source": [
116+
"map(lambda x: (5.0/9)*(x - 32), F_temps)"
117+
]
118+
},
119+
{
120+
"cell_type": "markdown",
121+
"metadata": {},
122+
"source": [
123+
"Great! We got the same result! Using map is much more commonly used with lambda expressions since the entire purpose of map() is to save effort on having to create manual for loops."
124+
]
125+
},
126+
{
127+
"cell_type": "markdown",
128+
"metadata": {},
129+
"source": [
130+
"map() can be applied to more than one iterable. The iterables have to have the same length.\n",
131+
"\n",
132+
"\n",
133+
"For instance, if we are working with two lists-map() will apply its lambda function to the elements of the argument lists, i.e. it first applies to the elements with the 0th index, then to the elements with the 1st index until the n-th index is reached.\n",
134+
"\n",
135+
"For example lets map a lambda expression to two lists:"
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": 12,
141+
"metadata": {
142+
"collapsed": false
143+
},
144+
"outputs": [
145+
{
146+
"data": {
147+
"text/plain": [
148+
"[6, 8, 10, 12]"
149+
]
150+
},
151+
"execution_count": 12,
152+
"metadata": {},
153+
"output_type": "execute_result"
154+
}
155+
],
156+
"source": [
157+
"a = [1,2,3,4]\n",
158+
"b = [5,6,7,8]\n",
159+
"c = [9,10,11,12]\n",
160+
"\n",
161+
"map(lambda x,y:x+y,a,b)"
162+
]
163+
},
164+
{
165+
"cell_type": "code",
166+
"execution_count": 13,
167+
"metadata": {
168+
"collapsed": false
169+
},
170+
"outputs": [
171+
{
172+
"data": {
173+
"text/plain": [
174+
"[15, 18, 21, 24]"
175+
]
176+
},
177+
"execution_count": 13,
178+
"metadata": {},
179+
"output_type": "execute_result"
180+
}
181+
],
182+
"source": [
183+
"# Now all three lists\n",
184+
"map(lambda x,y,z:x+y+z, a,b,c)"
185+
]
186+
},
187+
{
188+
"cell_type": "markdown",
189+
"metadata": {},
190+
"source": [
191+
"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",
192+
"\n",
193+
"Great job! You should now have a basic understanding of the map() function."
194+
]
195+
}
196+
],
197+
"metadata": {
198+
"kernelspec": {
199+
"display_name": "Python 3",
200+
"language": "python",
201+
"name": "python3"
202+
},
203+
"language_info": {
204+
"codemirror_mode": {
205+
"name": "ipython",
206+
"version": 3
207+
},
208+
"file_extension": ".py",
209+
"mimetype": "text/x-python",
210+
"name": "python",
211+
"nbconvert_exporter": "python",
212+
"pygments_lexer": "ipython3",
213+
"version": "3.5.1"
214+
}
215+
},
216+
"nbformat": 4,
217+
"nbformat_minor": 0
218+
}

Map.ipynb

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"#map()\n",
7+
"# map()\n",
88
"\n",
99
"map() is a function that takes in two arguments: a function and a sequence iterable. In the form:\n",
1010
" map(function, sequence)\n",
@@ -58,7 +58,7 @@
5858
}
5959
],
6060
"source": [
61-
"F_temps = map(Fahrenheit, temp)\n",
61+
"F_temps = map(fahrenheit, temp)\n",
6262
"\n",
6363
"#Show\n",
6464
"F_temps"
@@ -196,21 +196,21 @@
196196
],
197197
"metadata": {
198198
"kernelspec": {
199-
"display_name": "Python 2",
199+
"display_name": "Python 3",
200200
"language": "python",
201-
"name": "python2"
201+
"name": "python3"
202202
},
203203
"language_info": {
204204
"codemirror_mode": {
205205
"name": "ipython",
206-
"version": 2
206+
"version": 3
207207
},
208208
"file_extension": ".py",
209209
"mimetype": "text/x-python",
210210
"name": "python",
211211
"nbconvert_exporter": "python",
212-
"pygments_lexer": "ipython2",
213-
"version": "2.7.10"
212+
"pygments_lexer": "ipython3",
213+
"version": "3.5.1"
214214
}
215215
},
216216
"nbformat": 4,

0 commit comments

Comments
 (0)