Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 39d5157

Browse files
committed
create jupyter notebook for map()
1 parent 351768d commit 39d5157

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed

‎built_in_functions/map.ipynb

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# map()\n",
8+
"Map applies a function to all the items in an input_list. Here is the \n",
9+
"blueprint:\n",
10+
"\n",
11+
"```python\n",
12+
"map(function_to_apply, list_of_inputs)\n",
13+
"```"
14+
]
15+
},
16+
{
17+
"cell_type": "markdown",
18+
"metadata": {},
19+
"source": [
20+
"Most of the times we want to pass all the list elements to a function \n",
21+
"one-by-one and then collect the output. For instance:"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": 11,
27+
"metadata": {},
28+
"outputs": [
29+
{
30+
"name": "stdout",
31+
"output_type": "stream",
32+
"text": [
33+
"[1, 2, 3, 4, 5]\n",
34+
"[1, 4, 9, 16, 25]\n"
35+
]
36+
}
37+
],
38+
"source": [
39+
"items = [1, 2, 3, 4, 5]\n",
40+
"squared_items = []\n",
41+
"for i in items:\n",
42+
" squared_items.append(i**2)\n",
43+
" \n",
44+
"print(items)\n",
45+
"print(squared_items)"
46+
]
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"metadata": {},
51+
"source": [
52+
"Map allows us to implement this in a much simpler and nicer way. Here you go:"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": 12,
58+
"metadata": {},
59+
"outputs": [
60+
{
61+
"name": "stdout",
62+
"output_type": "stream",
63+
"text": [
64+
"[1, 2, 3, 4, 5]\n",
65+
"[1, 4, 9, 16, 25]\n"
66+
]
67+
}
68+
],
69+
"source": [
70+
"def square(x):\n",
71+
" return x**2\n",
72+
"\n",
73+
"items = [1, 2, 3, 4, 5]\n",
74+
"squared_items = list(map(square, items))\n",
75+
"\n",
76+
"print(items)\n",
77+
"print(squared_items)"
78+
]
79+
},
80+
{
81+
"cell_type": "markdown",
82+
"metadata": {},
83+
"source": [
84+
"Most of the times we use lambdas with `map`, so I did the same. "
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": 13,
90+
"metadata": {},
91+
"outputs": [
92+
{
93+
"name": "stdout",
94+
"output_type": "stream",
95+
"text": [
96+
"[1, 2, 3, 4, 5]\n",
97+
"[1, 4, 9, 16, 25]\n"
98+
]
99+
}
100+
],
101+
"source": [
102+
"items = [1, 2, 3, 4, 5]\n",
103+
"squared = list(map(lambda x: x**2, items))\n",
104+
"\n",
105+
"print(items)\n",
106+
"print(squared)"
107+
]
108+
},
109+
{
110+
"cell_type": "markdown",
111+
"metadata": {},
112+
"source": [
113+
"Instead of a list of inputs, we can even apply a list of functions!"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 16,
119+
"metadata": {},
120+
"outputs": [
121+
{
122+
"name": "stdout",
123+
"output_type": "stream",
124+
"text": [
125+
"[0, 0]\n",
126+
"[1, 2]\n",
127+
"[4, 4]\n",
128+
"[9, 6]\n",
129+
"[16, 8]\n"
130+
]
131+
}
132+
],
133+
"source": [
134+
"def square(x):\n",
135+
"\treturn (x*x)\n",
136+
"def double(x):\n",
137+
"\treturn (2*x)\n",
138+
"\n",
139+
"# For the numbers from 0 to 4, square it and double it\n",
140+
"funcs = [square, double]\n",
141+
"for i in range(5):\n",
142+
" # value will be a list containing two elements [i^2, 2*i], one for each function in funcs\n",
143+
" value = list(map(lambda x: x(i), funcs))\n",
144+
" print(value)"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": null,
150+
"metadata": {},
151+
"outputs": [],
152+
"source": []
153+
}
154+
],
155+
"metadata": {
156+
"kernelspec": {
157+
"display_name": "Python 3",
158+
"language": "python",
159+
"name": "python3"
160+
},
161+
"language_info": {
162+
"codemirror_mode": {
163+
"name": "ipython",
164+
"version": 3
165+
},
166+
"file_extension": ".py",
167+
"mimetype": "text/x-python",
168+
"name": "python",
169+
"nbconvert_exporter": "python",
170+
"pygments_lexer": "ipython3",
171+
"version": "3.6.4"
172+
}
173+
},
174+
"nbformat": 4,
175+
"nbformat_minor": 2
176+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /