{ "metadata": { "name": "", "signature": "sha256:f702e3f87bd5f74af86db9c6b163d38ab8c57f82c2e0546765ce3109ff60b864" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Apply Operations Over Items In A List" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- **Author:** [Chris Albon](http://www.chrisalbon.com/), [@ChrisAlbon](https://twitter.com/chrisalbon)\n", "- **Date:** -\n", "- **Repo:** [Python 3 code snippets for data science](https://github.com/chrisalbon/code_py)\n", "- **Note:** Based on: Learning Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Method 1: map()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a list of casualties from battles" ] }, { "cell_type": "code", "collapsed": false, "input": [ "battleDeaths = [482, 93, 392, 920, 813, 199, 374, 237, 244]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 20 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a function that updates all battle deaths by adding 100" ] }, { "cell_type": "code", "collapsed": false, "input": [ "def updated(x): return x + 100" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 21 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a list that applies updated() to all elements of battleDeaths" ] }, { "cell_type": "code", "collapsed": false, "input": [ "list(map(updated, battleDeaths))" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 22, "text": [ "[582, 193, 492, 1020, 913, 299, 474, 337, 344]" ] } ], "prompt_number": 22 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Method 2: for x in y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a list of deaths" ] }, { "cell_type": "code", "collapsed": false, "input": [ "casualties = [482, 93, 392, 920, 813, 199, 374, 237, 244]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 24 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a variable where we will put the updated casualty numbers" ] }, { "cell_type": "code", "collapsed": false, "input": [ "casualtiesUpdated = []" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 25 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a function that for each item in casualties, adds 10" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for x in casualties:\n", " casualtiesUpdated.append(x + 100)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 26 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View casualties variables" ] }, { "cell_type": "code", "collapsed": false, "input": [ "casualtiesUpdated" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 27, "text": [ "[582, 193, 492, 1020, 913, 299, 474, 337, 344]" ] } ], "prompt_number": 27 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Method 3: lambda functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Map the lambda function x() over casualties" ] }, { "cell_type": "code", "collapsed": false, "input": [ "list(map((lambda x: x + 100), casualties))" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 29, "text": [ "[582, 193, 492, 1020, 913, 299, 474, 337, 344]" ] } ], "prompt_number": 29 } ], "metadata": {} } ] }