{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python itertools\n", "> Swarm Intelligence from social interaction\n", "\n", "- toc: false \n", "- badges: true\n", "- hide: true\n", "- comments: true\n", "- image: images/copied_from_nb/my_icons/static_pso.png\n", "- categories: [jupyter, optimisation, visualisation]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Pre-requisits " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can pass functions as variables" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def add_two(x):\n", " return x+2\n", "\n", "def do_something_then_add_three(something_to_do_first, x):\n", " # first call something_to_do_first with the input, then add 3\n", " return something_to_do_first(x) + 3\n", "\n", "# We pass add_two (note the lack of brackets beside it)\n", "do_something_then_add_three(add_two, 0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "An iterator is an object representing a stream of data. You can call `next` on an iterator to get the next value." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'abcdefghijklmnopqrstuvwxyz'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from string import ascii_lowercase\n", "ascii_lowercase" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "iter(ascii_lowercase)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a'" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphabet_iterator = iter(ascii_lowercase)\n", "next(alphabet_iterator)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'b'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(alphabet_iterator)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def add_underscore(x,y): \n", " return f'{x}_{y}'" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "import itertools\n", "alphabet_iterator = iter(ascii_lowercase)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 3, 5]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_list = [1,3,5,6]\n", "list(itertools.islice(my_list, 3))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Say we want to create a list of tuples with ( position_in_alphabet, letter ) starting at (1, 'a') like" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(1, 'a'), (2, 'b'), (3, 'c')]" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[(1, 'a'), (2, 'b'), (3, 'c')]" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(1, 'a'),\n", " (2, 'b'),\n", " (3, 'c'),\n", " (4, 'd'),\n", " (5, 'e'),\n", " (6, 'f'),\n", " (7, 'g'),\n", " (8, 'h'),\n", " (9, 'i'),\n", " (10, 'j'),\n", " (11, 'k'),\n", " (12, 'l'),\n", " (13, 'm'),\n", " (14, 'n'),\n", " (15, 'o'),\n", " (16, 'p'),\n", " (17, 'q'),\n", " (18, 'r'),\n", " (19, 's'),\n", " (20, 't'),\n", " (21, 'u'),\n", " (22, 'v'),\n", " (23, 'w'),\n", " (24, 'x'),\n", " (25, 'y'),\n", " (26, 'z')]" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphabet_tuples = []\n", "for i in range(len(ascii_lowercase)):\n", " alphabet_tuples.append((i+1, ascii_lowercase[i])) \n", "alphabet_tuples" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(1, 'a'),\n", " (2, 'b'),\n", " (3, 'c'),\n", " (4, 'd'),\n", " (5, 'e'),\n", " (6, 'f'),\n", " (7, 'g'),\n", " (8, 'h'),\n", " (9, 'i'),\n", " (10, 'j'),\n", " (11, 'k'),\n", " (12, 'l'),\n", " (13, 'm'),\n", " (14, 'n'),\n", " (15, 'o'),\n", " (16, 'p'),\n", " (17, 'q'),\n", " (18, 'r'),\n", " (19, 's'),\n", " (20, 't'),\n", " (21, 'u'),\n", " (22, 'v'),\n", " (23, 'w'),\n", " (24, 'x'),\n", " (25, 'y'),\n", " (26, 'z')]" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(zip(count(start=1), ascii_lowercase))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import IPython.display as ipd\n", "import numpy as np\n", "import itertools" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`°º¤ø,¸¸,ø¤º°`'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import itertools\n", "\n", "def ascii_wav(times=3):\n", " wave = \"°º¤ø,¸¸,ø¤º°`\"\n", " return ''.join(itertools.repeat(wave, times))\n", "\n", "ascii_wav()" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.5" } }, "nbformat": 4, "nbformat_minor": 2 }