{ "metadata": { "name": "", "signature": "sha256:d77faebe0648df5fbcb30809e170e42b67c8738623a37316c585df4590c8f498" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Applying Conditional 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": 5 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using A Conditional For Loop" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a for loop goes through the list and capitalizes an item if the first letter starts with N" ] }, { "cell_type": "code", "collapsed": false, "input": [ "# create a variable for the loop results\n", "regimentNamesCapitalized_f = []\n", "\n", "# for every item in regimentNames\n", "for i in regimentNames:\n", " # if the first letter of the regiment's name is N\n", " if i[0] == \"N\":\n", " # capitalize the item and add it to regimentNamesCapitalized_f\n", " regimentNamesCapitalized_f.append(i.upper())\n", " # otherwise, add it to the new list without changing anything\n", " else:\n", " regimentNamesCapitalized_f.append(i)\n", " \n", "# View the outcome\n", "regimentNamesCapitalized_f" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 9, "text": [ "['NIGHT RIFLEMEN',\n", " 'Jungle Scouts',\n", " 'The Dragoons',\n", " 'Midnight Revengence',\n", " 'Wily Warriors']" ] } ], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Using List Comprehension With Conditionals" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Apply the expression x.upper to each item in the list called regiment names if it matches a condition, else apply a different function (or, in this case, do nothing)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "regimentNamesCapitalized_l =[x.upper() if x[0] == \"N\" else x for x in regimentNames]; regimentNamesCapitalized_l" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 24, "text": [ "['NIGHT RIFLEMEN',\n", " 'Jungle Scouts',\n", " 'The Dragoons',\n", " 'Midnight Revengence',\n", " 'Wily Warriors']" ] } ], "prompt_number": 24 }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that the syntax for conditional list comprehension is **[x if y else z for k in v]**\n", "\n", "- x = function if conditional statement is true\n", "- y = conditional statement\n", "- z = function if conditional statement is not true\n", "- k = items in list\n", "- v = list variable" ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }