{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "Lesson10-Python For Data Science-Functional-Programming.ipynb", "version": "0.3.2", "provenance": [], "collapsed_sections": [ "PohjG7oQgkzj", "SQ3i70ApGJQc", "FygmidaBkYYY", "BmnzjtIHGmLp", "iDRs8e5Zgpt5", "OJD2zsEje_8B" ], "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "metadata": { "id": "LmbpfDX-ejGN", "colab_type": "text" }, "cell_type": "markdown", "source": [ "# Lesson 10: Functional Programming \n" ] }, { "metadata": { "id": "c_Id55m6Jsbu", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## Pragmatic AI Labs\n", "\n" ] }, { "metadata": { "id": "e5p96AqpSDZa", "colab_type": "text" }, "cell_type": "markdown", "source": [ "![alt text](https://paiml.com/images/logo_with_slogan_white_background.png)\n", "\n", "This notebook was produced by [Pragmatic AI Labs](https://paiml.com/). You can continue learning about these topics by:\n", "\n", "* Buying a copy of [Pragmatic AI: An Introduction to Cloud-Based Machine Learning](http://www.informit.com/store/pragmatic-ai-an-introduction-to-cloud-based-machine-9780134863917)\n", "* Reading an online copy of [Pragmatic AI:Pragmatic AI: An Introduction to Cloud-Based Machine Learning](https://www.safaribooksonline.com/library/view/pragmatic-ai-an/9780134863924/)\n", "* Watching video [Essential Machine Learning and AI with Python and Jupyter Notebook-Video-SafariOnline](https://www.safaribooksonline.com/videos/essential-machine-learning/9780135261118) on Safari Books Online.\n", "* Watching video [AWS Certified Machine Learning-Speciality](https://learning.oreilly.com/videos/aws-certified-machine/9780135556597)\n", "* Purchasing video [Essential Machine Learning and AI with Python and Jupyter Notebook- Purchase Video](http://www.informit.com/store/essential-machine-learning-and-ai-with-python-and-jupyter-9780135261095)\n", "* Viewing more content at [noahgift.com](https://noahgift.com/)\n" ] }, { "metadata": { "id": "9eMAJeDEfali", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## 10.1 Understand functional programming\n", "\n" ] }, { "metadata": { "id": "PohjG7oQgkzj", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Depending on state" ] }, { "metadata": { "id": "OBRWaNjHi8Gb", "colab_type": "code", "outputId": "f6b292f2-97a0-4ca3-af08-50c25dcdd544", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "wind = 'Southeast'\n", "\n", "def describe_the_wind():\n", " return f'The wind blows from the {wind}'\n", "\n", "describe_the_wind()" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'The wind blows from the Southeast'" ] }, "metadata": { "tags": [] }, "execution_count": 2 } ] }, { "metadata": { "id": "SQ3i70ApGJQc", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Functional approach" ] }, { "metadata": { "id": "SoIA9ynnGLWo", "colab_type": "code", "outputId": "3b4b175d-eff3-4126-9586-dec0d7ad7b83", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "def describe_the_wind(wind):\n", " return f'The wind blows from the {wind}'\n", "\n", "describe_the_wind('Northeast')" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'The wind blows from the Northeast'" ] }, "metadata": { "tags": [] }, "execution_count": 4 } ] }, { "metadata": { "id": "FygmidaBkYYY", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Changing state" ] }, { "metadata": { "id": "50h4eF7okhsg", "colab_type": "code", "outputId": "38b5de32-f212-45f0-b22d-8a14bafab182", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "WINDS = ['Northeast', 'Northwest', 'Southeast', 'Southwest']\n", "WIND = WINDS[0]\n", "\n", "def change_wind():\n", " global WIND\n", " WIND = WINDS[(WINDS.index(WIND) + 1)%3]\n", " \n", "WIND" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Northeast'" ] }, "metadata": { "tags": [] }, "execution_count": 11 } ] }, { "metadata": { "id": "dxGKKBAo3-yw", "colab_type": "code", "outputId": "1229bd51-9777-48ab-d3b3-9a75ff5df991", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "change_wind()\n", "WIND" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "'Northeast'" ] }, "metadata": { "tags": [] }, "execution_count": 14 } ] }, { "metadata": { "id": "Qlf5tNksnZSH", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "for _ in WINDS:\n", " print(WIND)\n", " change_wind()\n" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "BmnzjtIHGmLp", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Functional approach" ] }, { "metadata": { "id": "8FUEdYrPGpI4", "colab_type": "code", "outputId": "0c5d8aeb-059a-46b4-eabb-89a2d08b1738", "colab": { "base_uri": "https://localhost:8080/", "height": 85 } }, "cell_type": "code", "source": [ "def change_wind(wind_index):\n", " winds = ['Northeast', 'Northwest', 'Southeast', 'Southwest']\n", " return winds[wind_index]\n", "\n", "print( change_wind(0) )\n", "print( change_wind(1) )\n", "print( change_wind(2) )\n", "print( change_wind(3) )" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "Northeast\n", "Northwest\n", "Southeast\n", "Southwest\n" ], "name": "stdout" } ] }, { "metadata": { "id": "iDRs8e5Zgpt5", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Changing state\n" ] }, { "metadata": { "id": "ER1QjmDLgzra", "colab_type": "code", "outputId": "a7ad1184-07c3-4b63-d60e-9129af234a67", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "def change_mutable_data(data):\n", " '''A function which changes\n", " mutable data.'''\n", " data['owner'] = 'White Star Line'\n", "\n", " \n", "d = {\"vehicle\": \"ship\", \"owner\": \"Joseph Bruce Ismay\"}\n", "\n", "change_mutable_data(d)\n", "d\n" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'owner': 'White Star Line', 'vehicle': 'ship'}" ] }, "metadata": { "tags": [] }, "execution_count": 16 } ] }, { "metadata": { "id": "OJD2zsEje_8B", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Functional approach" ] }, { "metadata": { "id": "h13Xi0XLfC-s", "colab_type": "code", "outputId": "7af39aab-b727-4fd2-dc13-b374bc37686a", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "d = {\"vehicle\": \"ship\", \"owner\": \"Joseph Bruce Ismay\"}\n", "\n", "def change_owner(data):\n", " new_data = data.copy()\n", " new_data['owner'] = 'White Star Line'\n", " return new_data\n", "\n", "\n", "changed = change_owner(d)\n", "changed" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'owner': 'White Star Line', 'vehicle': 'ship'}" ] }, "metadata": { "tags": [] }, "execution_count": 17 } ] }, { "metadata": { "id": "I_6q5sCLgn64", "colab_type": "text" }, "cell_type": "markdown", "source": [ "" ] }, { "metadata": { "id": "vrY3sqy1fcIa", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## 10.2 Apply functions to data science workflows \n" ] }, { "metadata": { "id": "tmr8KB2cpo2j", "colab_type": "code", "outputId": "43273926-d7c0-47f9-be92-1e66e64e7cac", "colab": { "base_uri": "https://localhost:8080/", "height": 142 } }, "cell_type": "code", "source": [ "import pandas as pd\n", "moby_dick_quotes = [\n", " \"\"\"It is not down on any map; true places never are.\"\"\",\n", " \"\"\"\n", " Consider the subtleness of the sea; how its most dreaded creatures glide under water, unapparent for the most part, and treacherously hidden beneath the loveliest tints of azure. Consider also the devilish brilliance and beauty of many of its most remorseless tribes, as the dainty embellished shape of many species of sharks. Consider, once more, the universal cannibalism of the sea; all whose creatures prey upon each other, carrying on eternal war since the world began. \n", " Consider all this; and then turn to the green, gentle, and most docile earth; consider them both, the sea and the land; and do you not find a strange analogy to something in yourself? For as this appalling ocean surrounds the verdant land, so in the soul of man there lies one insular Tahiti, full of peace and joy, but encompassed by all the horrors of the half-known life. God keep thee! Push not off from that isle, thou canst never return!\n", " \"\"\",\n", " \"\"\"\n", " The sea had jeeringly kept his finite body up, but drowned the infinite of his soul. Not drowned entirely, though. Rather carried down alive to wondrous depths, where strange shapes of the unwarped primal world glided to and fro before his passive eyes; and the miser-merman, Wisdom, revealed his hoarded heaps; and among the joyous, heartless, ever-juvenile eternities, Pip saw the multitudinous, God-omnipresent, coral insects, that out of the firmament of waters heaved the colossal orbs. He saw God’s foot upon the treadle of the loom, and spoke it; and therefore his shipmates called him mad. So man’s insanity is heaven’s sense; and wandering from all mortal reason, man comes at last to that celestial thought, which, to reason, is absurd and frantic; and weal or woe, feels then uncompromised, indifferent as his God.\n", " \"\"\"\n", "]\n", "df = pd.DataFrame(moby_dick_quotes)\n", "df.columns = [\"quotes\"]\n", "df.head()" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
quotes
0It is not down on any map; true places never are.
1\\n Consider the subtleness of the sea; how ...
2\\n The sea had jeeringly kept his finite bo...
\n", "
" ], "text/plain": [ " quotes\n", "0 It is not down on any map; true places never are.\n", "1 \\n Consider the subtleness of the sea; how ...\n", "2 \\n The sea had jeeringly kept his finite bo..." ] }, "metadata": { "tags": [] }, "execution_count": 18 } ] }, { "metadata": { "id": "3p81PUEAqoAX", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "def classify_religious(column):\n", " \"\"\"Creates Religion Feature\"\"\"\n", " \n", " if \"God\" in column:\n", " return \"Religious\"\n", " return \"Secular\"\n" ], "execution_count": 0, "outputs": [] }, { "metadata": { "id": "pGhepUzLrK0d", "colab_type": "code", "outputId": "70bfcd29-f747-4282-828b-715e3143459e", "colab": { "base_uri": "https://localhost:8080/", "height": 142 } }, "cell_type": "code", "source": [ "df[\"quote_type\"] = df[\"quotes\"].apply(classify_religious)\n", "df.head()\n", " " ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
quotesquote_type
0It is not down on any map; true places never are.Secular
1\\n Consider the subtleness of the sea; how ...Religious
2\\n The sea had jeeringly kept his finite bo...Religious
\n", "
" ], "text/plain": [ " quotes quote_type\n", "0 It is not down on any map; true places never are. Secular\n", "1 \\n Consider the subtleness of the sea; how ... Religious\n", "2 \\n The sea had jeeringly kept his finite bo... Religious" ] }, "metadata": { "tags": [] }, "execution_count": 20 } ] }, { "metadata": { "id": "6zh5svNAfdzO", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## 10.3 Use map/reduce/filter \n" ] }, { "metadata": { "id": "AzvEtVmOfp0v", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### map" ] }, { "metadata": { "id": "G4egV1e-pzpk", "colab_type": "code", "outputId": "056f0dad-08e9-4f07-b83a-06c7364910fa", "colab": { "base_uri": "https://localhost:8080/", "height": 119 } }, "cell_type": "code", "source": [ "def count_flower_petals(d):\n", " return f\"{d} petals counted so far\"\n", "\n", "counts = map(count_flower_petals, [0,1,2,3,4,5])\n", "\n", "\n", "list(counts)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['0 petals counted so far',\n", " '1 petals counted so far',\n", " '2 petals counted so far',\n", " '3 petals counted so far',\n", " '4 petals counted so far',\n", " '5 petals counted so far']" ] }, "metadata": { "tags": [] }, "execution_count": 21 } ] }, { "metadata": { "id": "Y2-i3xIsJB76", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### mapping with multiple inputs" ] }, { "metadata": { "id": "prTZtPBkqz8g", "colab_type": "code", "outputId": "862a6de4-0131-47c1-ada3-7291e3533ae9", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "l1 = [0,1,2,3,4]\n", "l2 = [10,9,8,7,6]\n", "\n", "def multi(d1, d2):\n", " return d1 * d2\n", "\n", "result = map(multi, l1, l2)\n", "print( list(result) )" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "[0, 9, 16, 21, 24]\n" ], "name": "stdout" } ] }, { "metadata": { "id": "yVcBd6uyfvmT", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### reduce" ] }, { "metadata": { "id": "_-bTj20Asd3o", "colab_type": "code", "outputId": "0abf7734-7fa2-48cb-fcd1-d8974813ac5a", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "initial_balance = 10000\n", "debits = [20, 40, 300, 3000, 1, 234]\n", "\n", "\n", "balance = initial_balance\n", "\n", "for debit in debits:\n", " balance -= debit\n", " \n", "balance\n" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "6405" ] }, "metadata": { "tags": [] }, "execution_count": 24 } ] }, { "metadata": { "id": "KeK7jjmUvkEZ", "colab_type": "code", "outputId": "8c489079-a9b9-4f82-c955-5d62ed9aa2d7", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "inital_balance = 10000\n", "debits = [20, 40, 300, 3000, 1, 234]\n", "from functools import reduce\n", "\n", "def minus(a, b):\n", " return a - b\n", "\n", "balance = reduce(minus, debits, initial_balance)\n", "balance" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "6405" ] }, "metadata": { "tags": [] }, "execution_count": 25 } ] }, { "metadata": { "id": "hSI7bmtJvU8l", "colab_type": "code", "outputId": "25646ec4-aee3-4287-b618-286d935c4af5", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "inital_balance = 10000\n", "debits = [20, 40, 300, 3000, 1, 234]\n", "\n", "from functools import reduce\n", "import operator\n", "\n", "print( reduce(operator.sub, debits, initial_balance) )" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "6405\n" ], "name": "stdout" } ] }, { "metadata": { "id": "39Se-LJ2fw_8", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### filter" ] }, { "metadata": { "id": "UYODbSkU6DNF", "colab_type": "code", "outputId": "99606e57-1ab3-48fa-9670-030c3a98ceaa", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "characters = ['C', 'b', 'c', 'A', 'b','P', 'g', 'S']\n", "\n", "def cap(a):\n", " return a.isupper()\n", "\n", "retval = filter(cap, characters)\n", "\n", "list(retval)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['C', 'A', 'P', 'S']" ] }, "metadata": { "tags": [] }, "execution_count": 27 } ] }, { "metadata": { "id": "rtW2cuRvffEg", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## 10.4 Use list comprehensions \n" ] }, { "metadata": { "id": "b46O0ik6D9OT", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### For loop" ] }, { "metadata": { "id": "PCcVdA25CPfQ", "colab_type": "code", "outputId": "56104467-63c3-4c7d-b8c3-f155a293290a", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "names = ['tim', 'tiger', 'tabassum', 'theodora', 'tanya']\n", "\n", "capd = []\n", "\n", "for name in names:\n", " capd.append(name.title())\n", " \n", "capd" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Tim', 'Tiger', 'Tabassum', 'Theodora', 'Tanya']" ] }, "metadata": { "tags": [] }, "execution_count": 29 } ] }, { "metadata": { "id": "gulM54lKEDY9", "colab_type": "text" }, "cell_type": "markdown", "source": [ "### Basic list comprehension" ] }, { "metadata": { "id": "QPDSmrUSDnMy", "colab_type": "code", "outputId": "629c4dd4-a8df-43b2-c341-274c4bd02758", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "names = ['tim', 'tiger', 'tabassum', 'theodora', 'tanya']\n", "\n", "capd = [x.title() for x in names]\n", "\n", "capd" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['Tim', 'Tiger', 'Tabassum', 'Theodora', 'Tanya']" ] }, "metadata": { "tags": [] }, "execution_count": 30 } ] }, { "metadata": { "id": "UifPgNFjHQus", "colab_type": "text" }, "cell_type": "markdown", "source": [ "#### Mapping" ] }, { "metadata": { "id": "6uBqchOGHVLB", "colab_type": "text" }, "cell_type": "markdown", "source": [ "##### map example" ] }, { "metadata": { "id": "MieT1VOdHXHF", "colab_type": "code", "outputId": "3f6955cf-e9df-4a2c-e167-5e16d18e976a", "colab": { "base_uri": "https://localhost:8080/", "height": 119 } }, "cell_type": "code", "source": [ "def count_flower_petals(d):\n", " return f\"{d} petals counted so far\"\n", "\n", "counts = map(count_flower_petals, [0,1,2,3,4,5])\n", "\n", "list(counts)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['0 petals counted so far',\n", " '1 petals counted so far',\n", " '2 petals counted so far',\n", " '3 petals counted so far',\n", " '4 petals counted so far',\n", " '5 petals counted so far']" ] }, "metadata": { "tags": [] }, "execution_count": 31 } ] }, { "metadata": { "id": "ftNKvYi6HYYI", "colab_type": "text" }, "cell_type": "markdown", "source": [ "##### mapping using list comprehension" ] }, { "metadata": { "id": "Y5kzj8hEHdYF", "colab_type": "code", "outputId": "4dd8d73d-d19f-4d7d-9020-5a32763b12ab", "colab": { "base_uri": "https://localhost:8080/", "height": 119 } }, "cell_type": "code", "source": [ "[f\"{x} petals counted so far\" for x in [0,1,2,3,4,5]]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['0 petals counted so far',\n", " '1 petals counted so far',\n", " '2 petals counted so far',\n", " '3 petals counted so far',\n", " '4 petals counted so far',\n", " '5 petals counted so far']" ] }, "metadata": { "tags": [] }, "execution_count": 32 } ] }, { "metadata": { "id": "1wfo3RaYEPQq", "colab_type": "text" }, "cell_type": "markdown", "source": [ "#### Filtering" ] }, { "metadata": { "id": "f4jGnQsnEaRE", "colab_type": "text" }, "cell_type": "markdown", "source": [ "##### filter function" ] }, { "metadata": { "id": "pR3R6GX0ERfV", "colab_type": "code", "outputId": "15c76e91-e1da-4a4e-d5eb-ee86f8b854d3", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "characters = ['C', 'b', 'c', 'A', 'b','P', 'g', 'S']\n", "\n", "def cap(a):\n", " return a.isupper()\n", "\n", "retval = filter(cap, characters)\n", "\n", "list(retval)" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['C', 'A', 'P', 'S']" ] }, "metadata": { "tags": [] }, "execution_count": 33 } ] }, { "metadata": { "id": "sXUpqOFtEeUU", "colab_type": "text" }, "cell_type": "markdown", "source": [ "##### filtering in list comprehension" ] }, { "metadata": { "id": "c3tfofEaEnC4", "colab_type": "code", "outputId": "7a553de6-3920-456d-de85-f5caa543c3fb", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "characters = ['C', 'b', 'c', 'A', 'b','P', 'g', 'S']\n", "\n", "[x for x in characters if x.isupper()]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['C', 'A', 'P', 'S']" ] }, "metadata": { "tags": [] }, "execution_count": 34 } ] }, { "metadata": { "id": "zWqshzzeJ397", "colab_type": "text" }, "cell_type": "markdown", "source": [ "#### Multiple variables" ] }, { "metadata": { "id": "gqj1EwKKKJT7", "colab_type": "code", "outputId": "456b8891-6689-458f-d9a0-339153d50c4a", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "horts = [ 32.0, 54.12, 12.4, 89.09]\n", "verts = [ 15.9, -34.21, 45.54, 90]\n", "\n", "[ f'x: {x} y: {y}' for x,y in zip(horts, verts) ]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "['x: 32.0 y: 15.9', 'x: 54.12 y: -34.21', 'x: 12.4 y: 45.54', 'x: 89.09 y: 90']" ] }, "metadata": { "tags": [] }, "execution_count": 35 } ] }, { "metadata": { "id": "HEy3hs-bLGIh", "colab_type": "code", "outputId": "c3040e7a-0cb8-49f6-a3e9-6b0abbd7bc2e", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "list(zip(horts, verts))" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[(32.0, 15.9), (54.12, -34.21), (12.4, 45.54), (89.09, 90)]" ] }, "metadata": { "tags": [] }, "execution_count": 36 } ] }, { "metadata": { "id": "P3-NfW0DM0Gw", "colab_type": "text" }, "cell_type": "markdown", "source": [ "#### Flatten a list of lists" ] }, { "metadata": { "id": "1bc22Je8MvZU", "colab_type": "code", "outputId": "ae690b79-d3cd-4607-cc6b-0bf0479d0d73", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "list_of_lists = [[1,2,3], [4,5,6], [7,8,9]]\n", "\n", "\n", "[x for y in list_of_lists for x in y]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "metadata": { "tags": [] }, "execution_count": 37 } ] }, { "metadata": { "id": "x3rJIsE8NG3R", "colab_type": "text" }, "cell_type": "markdown", "source": [ "#### Nested" ] }, { "metadata": { "id": "avQXm_V4NEad", "colab_type": "code", "outputId": "d6b8dc80-059f-4bc4-dd54-6adf2135b767", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "# Integers greater than and with 3 as a factor divided by 2\n", "\n", "\n", "[y for y in range(20)]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]" ] }, "metadata": { "tags": [] }, "execution_count": 38 } ] }, { "metadata": { "id": "OM8RDnApNbGp", "colab_type": "code", "outputId": "b2a16203-7a87-4584-abaa-46ce4aab41d3", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "[y for y in range(20) if y%3==0]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[0, 3, 6, 9, 12, 15, 18]" ] }, "metadata": { "tags": [] }, "execution_count": 39 } ] }, { "metadata": { "id": "PH0PNLTtNbKH", "colab_type": "code", "outputId": "130bd59b-a608-49c6-a32f-ea8b93023902", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "[x//2 for x in [y for y in range(20) if y%3==0] if x > 3]" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[3, 4, 6, 7, 9]" ] }, "metadata": { "tags": [] }, "execution_count": 40 } ] }, { "metadata": { "id": "pWGBUwC2fgY4", "colab_type": "text" }, "cell_type": "markdown", "source": [ "## 10.5 Use dictionary comprehensions " ] }, { "metadata": { "id": "QyRUr21wPpjq", "colab_type": "code", "outputId": "2d223089-2362-4627-bd80-1154d9a4b53d", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "cell_type": "code", "source": [ "rows = [{'name': 'barry', 'id': 1},\n", " {'name': 'henry', 'id': 3},\n", " {'name': 'suzy', 'id': 2}]\n", "\n", "{ x['name']: x['id'] for x in rows}\n" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'barry': 1, 'henry': 3, 'suzy': 2}" ] }, "metadata": { "tags": [] }, "execution_count": 42 } ] }, { "metadata": { "id": "OWvgobTeiWNy", "colab_type": "text" }, "cell_type": "markdown", "source": [ "# Notes\n", "[Functional Programming](https://en.wikipedia.org/wiki/Functional_programming)\n", "\n", "[Functional Programming Python](https://docs.python.org/3/howto/functional.html)" ] }, { "metadata": { "id": "7qyy6gUUCN1j", "colab_type": "code", "colab": {} }, "cell_type": "code", "source": [ "" ], "execution_count": 0, "outputs": [] } ] }