{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# list comprehension" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "{'Name1': 'Value1', 'Name2': 'Value2', 'Name3': 'Value3'}" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s= \"Name1=Value1;Name2=Value2;Name3=Value3\"\n", "dict(item.split(\"=\") for item in s.split(\";\"))" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "deletable": true, "editable": true }, "source": [ "# The map function\n", "- [Python Docs on the map function](https://docs.python.org/3.6/library/functions.html#map)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sq = lambda x:x**2\n", "num_list = [0,1,2,3,4,5,6,7,8,9]\n", "list(map(sq,num_list))" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "# The filter function" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "[3]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(filter(lambda n:n>0, [0,-1,3,-20]))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "[True, True, True, True, True]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(filter(lambda n:n, [True, True, True, False, True, True]))" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "['hello']" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_unfiltered_list = [True, True, True, False, True, True, \"hello\"]\n", "list(filter(lambda n:type(n)!= bool, my_unfiltered_list))" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "---\n", "# [itertools](https://docs.python.org/3/library/itertools.html#itertools.chain)\n", "\n", "## Use `product` from `itertools` to quickly iterate through all combinations of several lists" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "deletable": true, "editable": true }, "outputs": [], "source": [ "from itertools import product\n", "\n", "a = [\"foo\", \"melon\"]\n", "b = [True, False]\n", "c = [1,2,3]\n", "list(product(a, b, c))" ] }, { "cell_type": "markdown", "metadata": { "deletable": true, "editable": true }, "source": [ "# Use `itertools.starmap` to run a list of arguments through a given function." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "deletable": true, "editable": true }, "outputs": [ { "data": { "text/plain": [ "[4, 9, 16]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import itertools\n", "list(itertools.starmap(pow,[(2,2),(3,2),(4,2)]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "anaconda-cloud": {}, "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.6.0" } }, "nbformat": 4, "nbformat_minor": 2 }