|
| 1 | +{ |
| 2 | +"cells": [ |
| 3 | + { |
| 4 | +"cell_type":"markdown", |
| 5 | +"metadata": { |
| 6 | +"collapsed":true |
| 7 | + }, |
| 8 | +"source": [ |
| 9 | +"# LAMBDA, MAP, FILTER in PYTHON - source-nerd" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | +"cell_type":"markdown", |
| 14 | +"metadata": {}, |
| 15 | +"source": [ |
| 16 | +"<div class=\"alert alert-block alert-info\">\n", |
| 17 | +"<b>1. Lambda Function</b><br>\n", |
| 18 | +"It is a small anonymous function which has only one expression and can take n arguments.\n", |
| 19 | +"</div>\n", |
| 20 | +"> lambda arguments: expression\t" |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | +"cell_type":"code", |
| 25 | +"execution_count":3, |
| 26 | +"metadata": {}, |
| 27 | +"outputs": [ |
| 28 | + { |
| 29 | +"data": { |
| 30 | +"text/plain": [ |
| 31 | +"8" |
| 32 | + ] |
| 33 | + }, |
| 34 | +"execution_count":3, |
| 35 | +"metadata": {}, |
| 36 | +"output_type":"execute_result" |
| 37 | + } |
| 38 | + ], |
| 39 | +"source": [ |
| 40 | +"# Typical Add Function in Python\n", |
| 41 | +"def add(x, y):\n", |
| 42 | +" return x + y\n", |
| 43 | +"\n", |
| 44 | +"\n", |
| 45 | +"# Call the function\n", |
| 46 | +"add(3, 5)" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | +"cell_type":"code", |
| 51 | +"execution_count":5, |
| 52 | +"metadata": {}, |
| 53 | +"outputs": [ |
| 54 | + { |
| 55 | +"name":"stdout", |
| 56 | +"output_type":"stream", |
| 57 | +"text": [ |
| 58 | +"5\n" |
| 59 | + ] |
| 60 | + } |
| 61 | + ], |
| 62 | +"source": [ |
| 63 | +"# The above function converted to Lambda Function\n", |
| 64 | +"add_lambda = lambda x, y: x + y\n", |
| 65 | +"print(add_lambda(2, 3))\n" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | +"cell_type":"markdown", |
| 70 | +"metadata": {}, |
| 71 | +"source": [ |
| 72 | +"<div class=\"alert alert-block alert-info\">\n", |
| 73 | +"<b>2. Map Function</b><br>\n", |
| 74 | +"It applies a function to all items of the input.\n", |
| 75 | +"</div>\n", |
| 76 | +"> map(function_to_apply, list_of_inputs)" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | +"cell_type":"code", |
| 81 | +"execution_count":7, |
| 82 | +"metadata": {}, |
| 83 | +"outputs": [ |
| 84 | + { |
| 85 | +"data": { |
| 86 | +"text/plain": [ |
| 87 | +"[1, 4, 9, 16, 25]" |
| 88 | + ] |
| 89 | + }, |
| 90 | +"execution_count":7, |
| 91 | +"metadata": {}, |
| 92 | +"output_type":"execute_result" |
| 93 | + } |
| 94 | + ], |
| 95 | +"source": [ |
| 96 | +"# Typical function without map\n", |
| 97 | +"# In this example the square of items needs to be calculated and should be appended in a separate list\n", |
| 98 | +"items = [1, 2, 3, 4, 5]\n", |
| 99 | +"squared_items = []\n", |
| 100 | +"for x in items:\n", |
| 101 | +" squared_items.append(x**2)\n", |
| 102 | +"\n", |
| 103 | +"squared_items" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | +"cell_type":"code", |
| 108 | +"execution_count":9, |
| 109 | +"metadata": {}, |
| 110 | +"outputs": [ |
| 111 | + { |
| 112 | +"data": { |
| 113 | +"text/plain": [ |
| 114 | +"[1, 4, 9, 16, 25]" |
| 115 | + ] |
| 116 | + }, |
| 117 | +"execution_count":9, |
| 118 | +"metadata": {}, |
| 119 | +"output_type":"execute_result" |
| 120 | + } |
| 121 | + ], |
| 122 | +"source": [ |
| 123 | +"# The above function using map\n", |
| 124 | +"squared_using_map = list(map(lambda x: x**2, items))\n", |
| 125 | +"squared_using_map" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | +"cell_type":"markdown", |
| 130 | +"metadata": {}, |
| 131 | +"source": [ |
| 132 | +"<div class=\"alert alert-block alert-info\">\n", |
| 133 | +"<b>3. Filter Function</b><br>\n", |
| 134 | +"Returns a list of elements for which the function returns True.\n", |
| 135 | +"</div>\n", |
| 136 | +"> filter(function_to_apply, list_of_inputs)" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | +"cell_type":"code", |
| 141 | +"execution_count":10, |
| 142 | +"metadata": {}, |
| 143 | +"outputs": [ |
| 144 | + { |
| 145 | +"data": { |
| 146 | +"text/plain": [ |
| 147 | +"[2, 4]" |
| 148 | + ] |
| 149 | + }, |
| 150 | +"execution_count":10, |
| 151 | +"metadata": {}, |
| 152 | +"output_type":"execute_result" |
| 153 | + } |
| 154 | + ], |
| 155 | +"source": [ |
| 156 | +"# Typical function without filter\n", |
| 157 | +"# In this example, we only need the values from the list whose remainder is 0 when divided by 2\n", |
| 158 | +"filtered_items = []\n", |
| 159 | +"for item in items:\n", |
| 160 | +" if item % 2 == 0:\n", |
| 161 | +" filtered_items.append(item)\n", |
| 162 | +"\n", |
| 163 | +"filtered_items" |
| 164 | + ] |
| 165 | + }, |
| 166 | + { |
| 167 | +"cell_type":"code", |
| 168 | +"execution_count":14, |
| 169 | +"metadata": {}, |
| 170 | +"outputs": [ |
| 171 | + { |
| 172 | +"data": { |
| 173 | +"text/plain": [ |
| 174 | +"[2, 4]" |
| 175 | + ] |
| 176 | + }, |
| 177 | +"execution_count":14, |
| 178 | +"metadata": {}, |
| 179 | +"output_type":"execute_result" |
| 180 | + } |
| 181 | + ], |
| 182 | +"source": [ |
| 183 | +"# The above example using filter\n", |
| 184 | +"filtered_using_filter = list(filter(lambda x: x % 2 == 0, items))\n", |
| 185 | +"filtered_using_filter" |
| 186 | + ] |
| 187 | + } |
| 188 | + ], |
| 189 | +"metadata": { |
| 190 | +"kernelspec": { |
| 191 | +"display_name":"Python 2", |
| 192 | +"language":"python", |
| 193 | +"name":"python2" |
| 194 | + }, |
| 195 | +"language_info": { |
| 196 | +"codemirror_mode": { |
| 197 | +"name":"ipython", |
| 198 | +"version":2 |
| 199 | + }, |
| 200 | +"file_extension":".py", |
| 201 | +"mimetype":"text/x-python", |
| 202 | +"name":"python", |
| 203 | +"nbconvert_exporter":"python", |
| 204 | +"pygments_lexer":"ipython2", |
| 205 | +"version":"2.7.6" |
| 206 | + } |
| 207 | + }, |
| 208 | +"nbformat":4, |
| 209 | +"nbformat_minor":0 |
| 210 | +} |