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 8941975

Browse files
committed
finish filter, start reduce, start generators
1 parent 8141968 commit 8941975

File tree

9 files changed

+908
-0
lines changed

9 files changed

+908
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Map applies a function to all the items in an input_list. Here is the
2+
blueprint:
3+
4+
`map(function_to_apply, list_of_inputs)`
5+
6+
7+
Most of the times we want to pass all the list elements to a function
8+
one-by-one and then collect the output. For instance:
9+
10+
```
11+
items = [1, 2, 3, 4, 5]
12+
squared = []
13+
for i in items:
14+
squared.append(i**2)
15+
```
16+
17+
Map allows us to implement this in a much simpler and nicer way. Here you go:
18+
```
19+
items = [1, 2, 3, 4, 5]
20+
squared = list(map(lambda x: x**2, items))
21+
```
22+
23+
Most of the times we use lambdas with map, so I did the same. Instead of a
24+
list of inputs we can even have a list of functions!
25+
26+
```
27+
def multiply(x):
28+
return (x*x)
29+
def add(x):
30+
return (x+x)
31+
32+
funcs = [multiply, add]
33+
for i in range(5):
34+
value = list(map(lambda x: x(i), funcs))
35+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"cells": [],
3+
"metadata": {},
4+
"nbformat": 4,
5+
"nbformat_minor": 2
6+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Generators\n"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 3,
13+
"metadata": {},
14+
"outputs": [
15+
{
16+
"name": "stdout",
17+
"output_type": "stream",
18+
"text": [
19+
"0\n",
20+
"1\n",
21+
"2\n",
22+
"3\n",
23+
"4\n",
24+
"5\n",
25+
"6\n",
26+
"7\n",
27+
"8\n",
28+
"9\n"
29+
]
30+
}
31+
],
32+
"source": [
33+
"def gen():\n",
34+
" for i in range(10):\n",
35+
" yield i\n",
36+
"\n",
37+
"for num in gen():\n",
38+
" print(num)"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": null,
44+
"metadata": {},
45+
"outputs": [],
46+
"source": []
47+
}
48+
],
49+
"metadata": {
50+
"kernelspec": {
51+
"display_name": "Python 3",
52+
"language": "python",
53+
"name": "python3"
54+
},
55+
"language_info": {
56+
"codemirror_mode": {
57+
"name": "ipython",
58+
"version": 3
59+
},
60+
"file_extension": ".py",
61+
"mimetype": "text/x-python",
62+
"name": "python",
63+
"nbconvert_exporter": "python",
64+
"pygments_lexer": "ipython3",
65+
"version": "3.6.4"
66+
}
67+
},
68+
"nbformat": 4,
69+
"nbformat_minor": 2
70+
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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": 1,
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:"
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": 2,
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": 3,
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_items = list(map(lambda x: x**2, items))\n",
104+
"\n",
105+
"print(items)\n",
106+
"print(squared_items)"
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": 4,
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+
"metadata": {
149+
"kernelspec": {
150+
"display_name": "Python 3",
151+
"language": "python",
152+
"name": "python3"
153+
},
154+
"language_info": {
155+
"codemirror_mode": {
156+
"name": "ipython",
157+
"version": 3
158+
},
159+
"file_extension": ".py",
160+
"mimetype": "text/x-python",
161+
"name": "python",
162+
"nbconvert_exporter": "python",
163+
"pygments_lexer": "ipython3",
164+
"version": "3.6.4"
165+
}
166+
},
167+
"nbformat": 4,
168+
"nbformat_minor": 2
169+
}

0 commit comments

Comments
(0)

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