|
5 | 5 | "metadata": {},
|
6 | 6 | "source": [
|
7 | 7 | "# reduce()\n",
|
8 |
| - "As the name suggests, filter creates a list of elements for which a function returns true. Here is a simple example:" |
| 8 | + "\n", |
| 9 | + "`reduce(function_to_apply, list_to_apply_to)` is a really useful function for performing some computation on a list and returning the result. It applies a rolling computation to sequential pairs of values in a list. For example, if you wanted to compute the product of a list of integers.\n", |
| 10 | + "\n", |
| 11 | + "So the normal way you might go about doing this task in python is using a basic for loop:" |
9 | 12 | ]
|
10 | 13 | },
|
11 | 14 | {
|
12 | 15 | "cell_type": "code",
|
13 |
| - "execution_count": 13, |
| 16 | + "execution_count": 4, |
14 | 17 | "metadata": {},
|
15 | 18 | "outputs": [
|
16 | 19 | {
|
17 | 20 | "name": "stdout",
|
18 | 21 | "output_type": "stream",
|
19 | 22 | "text": [
|
20 |
| - "[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]\n", |
21 |
| - "[-5, -4, -3, -2, -1]\n" |
| 23 | + "24\n" |
22 | 24 | ]
|
23 | 25 | }
|
24 | 26 | ],
|
25 | 27 | "source": [
|
26 |
| - "def negative(x):\n", |
27 |
| - " return x < 0\n", |
28 |
| - "\n", |
29 |
| - "number_list = list(range(-5, 5))\n", |
30 |
| - "less_than_zero = list(filter(negative, number_list))\n", |
| 28 | + "product = 1\n", |
| 29 | + "list = [1, 2, 3, 4]\n", |
| 30 | + "for num in list:\n", |
| 31 | + " product = product * num\n", |
31 | 32 | "\n",
|
32 |
| - "print(number_list)\n", |
33 |
| - "print(less_than_zero)" |
| 33 | + "print(product)" |
34 | 34 | ]
|
35 | 35 | },
|
36 | 36 | {
|
37 | 37 | "cell_type": "markdown",
|
38 | 38 | "metadata": {},
|
39 | 39 | "source": [
|
40 |
| - "Just like `map()` you can use a lambda function for more concise code instead of defining one separately." |
| 40 | + "Now let’s try it with `reduce()`:" |
41 | 41 | ]
|
42 | 42 | },
|
43 | 43 | {
|
44 | 44 | "cell_type": "code",
|
45 |
| - "execution_count": 14, |
| 45 | + "execution_count": 5, |
46 | 46 | "metadata": {},
|
47 | 47 | "outputs": [
|
48 | 48 | {
|
49 | 49 | "name": "stdout",
|
50 | 50 | "output_type": "stream",
|
51 | 51 | "text": [
|
52 |
| - "[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]\n", |
53 |
| - "[-5, -4, -3, -2, -1]\n" |
| 52 | + "24\n" |
54 | 53 | ]
|
55 | 54 | }
|
56 | 55 | ],
|
57 | 56 | "source": [
|
58 |
| - "number_list = list(range(-5, 5))\n", |
59 |
| - "less_than_zero = list(filter(lambda x: x < 0, number_list))\n", |
| 57 | + "def multiply(x, y):\n", |
| 58 | + " return x*y\n", |
60 | 59 | "\n",
|
61 |
| - "print(number_list)\n", |
62 |
| - "print(less_than_zero)" |
| 60 | + "from functools import reduce\n", |
| 61 | + "product = reduce(multiply, [1, 2, 3, 4])\n", |
| 62 | + "print(product)" |
63 | 63 | ]
|
64 | 64 | },
|
65 | 65 | {
|
66 | 66 | "cell_type": "markdown",
|
67 | 67 | "metadata": {},
|
68 | 68 | "source": [
|
69 |
| - "The filter resembles a for loop but it is a builtin function and faster.\n", |
70 |
| - "Note: If map & filter do not appear beautiful to you then you can also use \n", |
71 |
| - " list/dict/tuple comprehensions." |
| 69 | + "Just like `filter()` and `map()`, `reduce()` takes a function to apply, so we can use a `lambda` function\n", |
| 70 | + "for more concise code:" |
72 | 71 | ]
|
73 | 72 | },
|
74 | 73 | {
|
75 | 74 | "cell_type": "code",
|
76 |
| - "execution_count": 15, |
| 75 | + "execution_count": 7, |
77 | 76 | "metadata": {},
|
78 | 77 | "outputs": [
|
79 | 78 | {
|
80 | 79 | "name": "stdout",
|
81 | 80 | "output_type": "stream",
|
82 | 81 | "text": [
|
83 |
| - "[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]\n", |
84 |
| - "[-5, -4, -3, -2, -1]\n" |
| 82 | + "24\n" |
85 | 83 | ]
|
86 | 84 | }
|
87 | 85 | ],
|
88 | 86 | "source": [
|
89 |
| - "number_list = list(range(-5, 5))\n", |
90 |
| - "less_than_zero = [x for x in number_list if x < 0]\n", |
91 |
| - "\n", |
92 |
| - "print(number_list)\n", |
93 |
| - "print(less_than_zero)" |
| 87 | + "from functools import reduce\n", |
| 88 | + "product = reduce((lambda x, y: x * y), [1, 2, 3, 4])\n", |
| 89 | + "print(product)" |
94 | 90 | ]
|
95 | 91 | },
|
96 | 92 | {
|
97 | 93 | "cell_type": "markdown",
|
98 | 94 | "metadata": {},
|
99 | 95 | "source": [
|
100 |
| - "You can use `filter()` for many things that require some condition, but just to give another example, you\n", |
101 |
| - "can use it to get just the even or odd numbers of a list" |
| 96 | + "You can use `reduce()` for a lot of things, so I'll just give a concise example for creating a\n", |
| 97 | + "Factorial function:" |
102 | 98 | ]
|
103 | 99 | },
|
104 | 100 | {
|
105 | 101 | "cell_type": "code",
|
106 |
| - "execution_count": 1, |
| 102 | + "execution_count": 26, |
107 | 103 | "metadata": {},
|
108 | 104 | "outputs": [
|
109 | 105 | {
|
110 | 106 | "name": "stdout",
|
111 | 107 | "output_type": "stream",
|
112 | 108 | "text": [
|
113 |
| - "[-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]\n", |
114 |
| - "[-4, -2, 0, 2, 4]\n", |
115 |
| - "[-5, -3, -1, 1, 3]\n" |
| 109 | + "120\n" |
116 | 110 | ]
|
117 | 111 | }
|
118 | 112 | ],
|
119 | 113 | "source": [
|
120 |
| - "number_list = list(range(-5, 5))\n", |
121 |
| - "even_numbers = list(filter(lambda x : x % 2 == 0, number_list))\n", |
122 |
| - "odd_numbers = list(filter(lambda x : x % 2 == 1, number_list))\n", |
| 114 | + "def factorial(n):\n", |
| 115 | + " return reduce((lambda x,y : x*y), range(1, n+1))\n", |
123 | 116 | "\n",
|
124 |
| - "print(number_list)\n", |
125 |
| - "print(even_numbers)\n", |
126 |
| - "print(odd_numbers)" |
| 117 | + "print(factorial(5))" |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "cell_type": "markdown", |
| 122 | + "metadata": {}, |
| 123 | + "source": [ |
| 124 | + "This is the same as using a for loop, but in one line instead of four:" |
127 | 125 | ]
|
128 | 126 | },
|
129 | 127 | {
|
130 | 128 | "cell_type": "code",
|
131 |
| - "execution_count": 3, |
| 129 | + "execution_count": 25, |
132 | 130 | "metadata": {},
|
133 | 131 | "outputs": [
|
134 | 132 | {
|
135 |
| - "ename": "TypeError", |
136 |
| - "evalue": "'int' object is not iterable", |
137 |
| - "output_type": "error", |
138 |
| - "traceback": [ |
139 |
| - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
140 |
| - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", |
141 |
| - "\u001b[0;32m<ipython-input-3-1da792b5c7a0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 7\u001b[0;31m \u001b[0;32mfor\u001b[0m \u001b[0mnum\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mgen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 8\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
142 |
| - "\u001b[0;31mTypeError\u001b[0m: 'int' object is not iterable" |
| 133 | + "name": "stdout", |
| 134 | + "output_type": "stream", |
| 135 | + "text": [ |
| 136 | + "120\n" |
143 | 137 | ]
|
144 | 138 | }
|
145 | 139 | ],
|
146 | 140 | "source": [
|
147 |
| - "number_list = list(range(-5, 5))\n", |
148 |
| - "\n", |
149 |
| - "def gen():\n", |
150 |
| - " for i in range(10):\n", |
151 |
| - " yield i\n", |
| 141 | + "def factorial(n):\n", |
| 142 | + " product = 1\n", |
| 143 | + " for i in range(1, n+1):\n", |
| 144 | + " product *= i\n", |
| 145 | + " return product\n", |
152 | 146 | "\n",
|
153 |
| - "for num in gen():\n", |
154 |
| - " print(num)\n" |
| 147 | + "print(factorial(5))" |
155 | 148 | ]
|
156 |
| - }, |
157 |
| - { |
158 |
| - "cell_type": "code", |
159 |
| - "execution_count": null, |
160 |
| - "metadata": {}, |
161 |
| - "outputs": [], |
162 |
| - "source": [] |
163 | 149 | }
|
164 | 150 | ],
|
165 | 151 | "metadata": {
|
|
0 commit comments