{ "metadata": { "name": "", "signature": "sha256:3e5aa0fcfb2ad33a45ef593467f89079c658c9f7ca5e2b4c3f7d09803f2dea06" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Applying Functions To List Items\n", "\n", "- **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:** -" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a list of regiment names" ] }, { "cell_type": "code", "collapsed": false, "input": [ "regimentNames = ['Night Riflemen', 'Jungle Scouts', 'The Dragoons', 'Midnight Revengence', 'Wily Warriors']" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 23 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using A For Loop" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a for loop goes through the list and capitalizes each" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# create a variable for the for loop results\n", "regimentNamesCapitalized_f = []\n", "\n", "# for every item in regimentNames\n", "for i in regimentNames:\n", " # capitalize the item and add it to regimentNamesCapitalized_f\n", " regimentNamesCapitalized_f.append(i.upper())\n", " \n", "# View the outcome\n", "regimentNamesCapitalized_f" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 26, "text": [ "['NIGHT RIFLEMEN',\n", " 'JUNGLE SCOUTS',\n", " 'THE DRAGOONS',\n", " 'MIDNIGHT REVENGENCE',\n", " 'WILY WARRIORS']" ] } ], "prompt_number": 26 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using Map()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a lambda function that capitalizes x" ] }, { "cell_type": "code", "collapsed": false, "input": [ "capitalizer = lambda x: x.upper()" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 25 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Map the capitalizer function to regimentNames, convert the map into a list, and view the variable" ] }, { "cell_type": "code", "collapsed": false, "input": [ "regimentNamesCapitalized_m = list(map(capitalizer, regimentNames)); regimentNamesCapitalized_m" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 18, "text": [ "['NIGHT RIFLEMEN',\n", " 'JUNGLE SCOUTS',\n", " 'THE DRAGOONS',\n", " 'MIDNIGHT REVENGENCE',\n", " 'WILY WARRIORS']" ] } ], "prompt_number": 18 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using List Comprehension" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Apply the expression x.upper to each item in the list called regiment names. Then view the output" ] }, { "cell_type": "code", "collapsed": false, "input": [ "regimentNamesCapitalized_l = [x.upper() for x in regimentNames]; regimentNamesCapitalized_l" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 22, "text": [ "['NIGHT RIFLEMEN',\n", " 'JUNGLE SCOUTS',\n", " 'THE DRAGOONS',\n", " 'MIDNIGHT REVENGENCE',\n", " 'WILY WARRIORS']" ] } ], "prompt_number": 22 } ], "metadata": {} } ] }