{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# *lambda* *X:* *f(X)* \n", "\n", "- parameters can be 1, or multiple\n", "- returns a function\n", "\n", "\n", "# *map(Function, X)\n", "\n", "\n", "# *zip* \n", "### - just like zipper, merges into pairs\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## simplest example" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def double(x):\n", " return x*2\n", "double(3)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "f = lambda x:2*x" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(3)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def add(x,y):\n", " return x+y\n", "add(2,3)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = lambda x,y: x+y\n", "f(2,3)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def larger(x,y):\n", " if x>y:\n", " return x\n", " else:\n", " return y\n", "larger(1,10)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f= lambda x,y:x if x>y else y\n", "f(1,10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Slightly more complex" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 2, 9, 4, 9]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = [1, 2, 3, 4, 5]\n", "b = [2, 2, 9, 0, 9]\n", "def pick_the_larger(x,y):\n", " result = [] # A list of the largest values\n", "\n", " # Assume both lists are the same length\n", " list_length = len(x)\n", " for i in range(list_length):\n", " result.append(max(x[i], y[i]))\n", " return result\n", "pick_the_larger(a,b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### zip\n", "Very much like what a zipper does, zip takes two equal-length collections and merge them into pairs. " ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "zip(a, b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### map\n", "*map(some_function, some_iterable)*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### More pythonic code - list comprehensions \n", "Faster performance: the less helper functions and the more language features" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 2, 9, 4, 9]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(map(max, zip(a,b)))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 2, 9, 4, 9]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[max(pair) for pair in zip(a,b)]" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 2, 9, 4, 9]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[max(ai,bi) for ai, bi in zip(a,b)]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 2, 9, 4, 9]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# in this case, max works on iterable too\n", "list(map(max,a,b))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 2, 9, 4, 9]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[max(a[x],b[x]) for x in range(len(a))]" ] } ], "metadata": { "kernelspec": { "display_name": "Python [default]", "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.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }