|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Python Lambda Functions\n", |
| 8 | + "Anonymous, single-use, or throw-away functions. \n", |
| 9 | + "**lambda arguments : expression** \n", |
| 10 | + "Here are some single-argument lambdas:" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 59, |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [ |
| 18 | + { |
| 19 | + "name": "stdout", |
| 20 | + "output_type": "stream", |
| 21 | + "text": [ |
| 22 | + "12\n" |
| 23 | + ] |
| 24 | + } |
| 25 | + ], |
| 26 | + "source": [ |
| 27 | + "add5 = lambda x: x + 5\n", |
| 28 | + "print(add5(7))" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": 58, |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [ |
| 36 | + { |
| 37 | + "name": "stdout", |
| 38 | + "output_type": "stream", |
| 39 | + "text": [ |
| 40 | + "64\n" |
| 41 | + ] |
| 42 | + } |
| 43 | + ], |
| 44 | + "source": [ |
| 45 | + "square = lambda x: x * x\n", |
| 46 | + "print(square(8))" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": 67, |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [ |
| 54 | + { |
| 55 | + "name": "stdout", |
| 56 | + "output_type": "stream", |
| 57 | + "text": [ |
| 58 | + "4\n", |
| 59 | + "3\n" |
| 60 | + ] |
| 61 | + } |
| 62 | + ], |
| 63 | + "source": [ |
| 64 | + "get_tens = lambda p: int(p/10)%10\n", |
| 65 | + "print(get_tens(749))\n", |
| 66 | + "print(get_tens(836.21))" |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "markdown", |
| 71 | + "metadata": {}, |
| 72 | + "source": [ |
| 73 | + "**Lambdas as an argument in other functions** \n", |
| 74 | + "One of the most popular uses for lambda functions is as an argument inside sort, or filter functions. \n", |
| 75 | + "### Sorting a List of Tuples using Lambda" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": 80, |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [ |
| 83 | + { |
| 84 | + "name": "stdout", |
| 85 | + "output_type": "stream", |
| 86 | + "text": [ |
| 87 | + "[('carrots', 1.1), ('peaches', 2.45), ('eggs', 5.25), ('honey', 9.7)]\n" |
| 88 | + ] |
| 89 | + } |
| 90 | + ], |
| 91 | + "source": [ |
| 92 | + "list1 = [('eggs', 5.25), ('honey', 9.70), ('carrots', 1.10), ('peaches', 2.45)]\n", |
| 93 | + "list1.sort(key = lambda x: x[1])\n", |
| 94 | + "print(list1)" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "markdown", |
| 99 | + "metadata": {}, |
| 100 | + "source": [ |
| 101 | + "### Sorting a List of Dictionaries using Lambda" |
| 102 | + ] |
| 103 | + }, |
| 104 | + { |
| 105 | + "cell_type": "code", |
| 106 | + "execution_count": 63, |
| 107 | + "metadata": {}, |
| 108 | + "outputs": [ |
| 109 | + { |
| 110 | + "name": "stdout", |
| 111 | + "output_type": "stream", |
| 112 | + "text": [ |
| 113 | + "[{'make': 'Tesla', 'model': 'X', 'year': 1999},\n", |
| 114 | + " {'make': 'Mercedes', 'model': 'C350E', 'year': 2008},\n", |
| 115 | + " {'make': 'Ford', 'model': 'Focus', 'year': 2013}]\n" |
| 116 | + ] |
| 117 | + } |
| 118 | + ], |
| 119 | + "source": [ |
| 120 | + "import pprint as pp\n", |
| 121 | + "list1 = [{'make':'Ford', 'model':'Focus', 'year':2013}, {'make':'Tesla', 'model':'X', 'year':1999}, {'make':'Mercedes', 'model':'C350E', 'year':2008}]\n", |
| 122 | + "list2 = sorted(list1, key = lambda x: x['year'])\n", |
| 123 | + "pp.pprint(list2)" |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "markdown", |
| 128 | + "metadata": {}, |
| 129 | + "source": [ |
| 130 | + "### Filtering a List of Integers using Lambda" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "code", |
| 135 | + "execution_count": 64, |
| 136 | + "metadata": {}, |
| 137 | + "outputs": [ |
| 138 | + { |
| 139 | + "name": "stdout", |
| 140 | + "output_type": "stream", |
| 141 | + "text": [ |
| 142 | + "[2, 4, 6]\n" |
| 143 | + ] |
| 144 | + } |
| 145 | + ], |
| 146 | + "source": [ |
| 147 | + "list1 = [1, 2, 3, 4, 5, 6]\n", |
| 148 | + "list2 = list(filter(lambda x: x%2 == 0, list1))\n", |
| 149 | + "print(list2)" |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + "cell_type": "code", |
| 154 | + "execution_count": 49, |
| 155 | + "metadata": {}, |
| 156 | + "outputs": [ |
| 157 | + { |
| 158 | + "name": "stdout", |
| 159 | + "output_type": "stream", |
| 160 | + "text": [ |
| 161 | + "[1, 3, 5]\n" |
| 162 | + ] |
| 163 | + } |
| 164 | + ], |
| 165 | + "source": [ |
| 166 | + "odds = lambda x: x%2 == 1\n", |
| 167 | + "list1 = [1, 2, 3, 4, 5, 6]\n", |
| 168 | + "list2 = list(filter(odds, list1))\n", |
| 169 | + "print(list2)" |
| 170 | + ] |
| 171 | + }, |
| 172 | + { |
| 173 | + "cell_type": "markdown", |
| 174 | + "metadata": {}, |
| 175 | + "source": [ |
| 176 | + "### Lambda Function on a List using Map\n", |
| 177 | + "Python's map function applies the lambda to every element in the list." |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "code", |
| 182 | + "execution_count": 74, |
| 183 | + "metadata": {}, |
| 184 | + "outputs": [ |
| 185 | + { |
| 186 | + "name": "stdout", |
| 187 | + "output_type": "stream", |
| 188 | + "text": [ |
| 189 | + "[1, 4, 9, 16, 25, 36]\n" |
| 190 | + ] |
| 191 | + } |
| 192 | + ], |
| 193 | + "source": [ |
| 194 | + "list1 = [1, 2, 3, 4, 5, 6]\n", |
| 195 | + "list2 = list(map(lambda x: x ** 2, list1))\n", |
| 196 | + "print(list2)" |
| 197 | + ] |
| 198 | + }, |
| 199 | + { |
| 200 | + "cell_type": "markdown", |
| 201 | + "metadata": {}, |
| 202 | + "source": [ |
| 203 | + "### Lambda Conditionals\n", |
| 204 | + "**lambda args: a if boolean_expression else b** " |
| 205 | + ] |
| 206 | + }, |
| 207 | + { |
| 208 | + "cell_type": "code", |
| 209 | + "execution_count": 70, |
| 210 | + "metadata": {}, |
| 211 | + "outputs": [ |
| 212 | + { |
| 213 | + "name": "stdout", |
| 214 | + "output_type": "stream", |
| 215 | + "text": [ |
| 216 | + "True\n" |
| 217 | + ] |
| 218 | + } |
| 219 | + ], |
| 220 | + "source": [ |
| 221 | + "starts_with_J = lambda x: True if x.startswith('J') else False\n", |
| 222 | + "print(starts_with_J('Joey'))" |
| 223 | + ] |
| 224 | + }, |
| 225 | + { |
| 226 | + "cell_type": "code", |
| 227 | + "execution_count": 81, |
| 228 | + "metadata": {}, |
| 229 | + "outputs": [ |
| 230 | + { |
| 231 | + "name": "stdout", |
| 232 | + "output_type": "stream", |
| 233 | + "text": [ |
| 234 | + "and\n" |
| 235 | + ] |
| 236 | + } |
| 237 | + ], |
| 238 | + "source": [ |
| 239 | + "wordb4 = lambda s, w: s.split()[s.split().index(w)-1] if w in s else None\n", |
| 240 | + "sentence = 'Four score and seven years ago'\n", |
| 241 | + "print(wordb4(sentence, 'seven'))" |
| 242 | + ] |
| 243 | + }, |
| 244 | + { |
| 245 | + "cell_type": "markdown", |
| 246 | + "metadata": {}, |
| 247 | + "source": [ |
| 248 | + "### Lambdas on DataTime Objects\n", |
| 249 | + "You sometimes want to get just the year, month, date or time for comparision. \n", |
| 250 | + "This would typically be most useful as a parameter in sort or filter functions." |
| 251 | + ] |
| 252 | + }, |
| 253 | + { |
| 254 | + "cell_type": "code", |
| 255 | + "execution_count": 82, |
| 256 | + "metadata": {}, |
| 257 | + "outputs": [ |
| 258 | + { |
| 259 | + "name": "stdout", |
| 260 | + "output_type": "stream", |
| 261 | + "text": [ |
| 262 | + "2019-03-07 19:36:58.442863\n", |
| 263 | + "2019\n" |
| 264 | + ] |
| 265 | + } |
| 266 | + ], |
| 267 | + "source": [ |
| 268 | + "import datetime\n", |
| 269 | + "\n", |
| 270 | + "now = datetime.datetime.now()\n", |
| 271 | + "print(now)\n", |
| 272 | + "year = lambda x: x.year\n", |
| 273 | + "print(year(now))" |
| 274 | + ] |
| 275 | + }, |
| 276 | + { |
| 277 | + "cell_type": "code", |
| 278 | + "execution_count": 79, |
| 279 | + "metadata": {}, |
| 280 | + "outputs": [ |
| 281 | + { |
| 282 | + "name": "stdout", |
| 283 | + "output_type": "stream", |
| 284 | + "text": [ |
| 285 | + "4096\n", |
| 286 | + "125\n" |
| 287 | + ] |
| 288 | + } |
| 289 | + ], |
| 290 | + "source": [ |
| 291 | + "def do_something(f, val):\n", |
| 292 | + " return f(val)\n", |
| 293 | + "\n", |
| 294 | + "func = lambda x: x**3\n", |
| 295 | + "print(func(16))\n", |
| 296 | + "print(do_something(func, 5))" |
| 297 | + ] |
| 298 | + }, |
| 299 | + { |
| 300 | + "cell_type": "markdown", |
| 301 | + "metadata": {}, |
| 302 | + "source": [ |
| 303 | + "### Extreme Lambdas\n", |
| 304 | + "This is probably a stretch -- you shouldn't be trying to do this much with Lambdas. \n", |
| 305 | + "Some things are better done in a regular function. But this shows what's possible with Lambdas." |
| 306 | + ] |
| 307 | + }, |
| 308 | + { |
| 309 | + "cell_type": "code", |
| 310 | + "execution_count": 66, |
| 311 | + "metadata": {}, |
| 312 | + "outputs": [ |
| 313 | + { |
| 314 | + "name": "stdout", |
| 315 | + "output_type": "stream", |
| 316 | + "text": [ |
| 317 | + "True\n", |
| 318 | + "True\n", |
| 319 | + "False\n", |
| 320 | + "False\n", |
| 321 | + "True\n", |
| 322 | + "-1\n", |
| 323 | + "-21.67 <class 'float'>\n" |
| 324 | + ] |
| 325 | + } |
| 326 | + ], |
| 327 | + "source": [ |
| 328 | + "isnum = lambda q: q.replace('.','',1).isdigit()\n", |
| 329 | + "print(isnum('25983'))\n", |
| 330 | + "print(isnum('3.1415'))\n", |
| 331 | + "print(isnum('T57'))\n", |
| 332 | + "print(isnum('-16'))\n", |
| 333 | + "\n", |
| 334 | + "is_num = lambda r: isnum(r[1:]) if r[0]=='-' else isnum(r)\n", |
| 335 | + "print(is_num('-16.4'))\n", |
| 336 | + "\n", |
| 337 | + "tonum = lambda s: float(s) if is_num(s) else -1\n", |
| 338 | + "print(tonum('30y'))\n", |
| 339 | + "print(tonum('-21.67'), type(tonum('-21.67')))" |
| 340 | + ] |
| 341 | + } |
| 342 | + ], |
| 343 | + "metadata": { |
| 344 | + "kernelspec": { |
| 345 | + "display_name": "Python 3", |
| 346 | + "language": "python", |
| 347 | + "name": "python3" |
| 348 | + }, |
| 349 | + "language_info": { |
| 350 | + "codemirror_mode": { |
| 351 | + "name": "ipython", |
| 352 | + "version": 3 |
| 353 | + }, |
| 354 | + "file_extension": ".py", |
| 355 | + "mimetype": "text/x-python", |
| 356 | + "name": "python", |
| 357 | + "nbconvert_exporter": "python", |
| 358 | + "pygments_lexer": "ipython3", |
| 359 | + "version": "3.7.0" |
| 360 | + } |
| 361 | + }, |
| 362 | + "nbformat": 4, |
| 363 | + "nbformat_minor": 2 |
| 364 | +} |
0 commit comments