{ "metadata": { "name": "", "signature": "sha256:58b4462d236013cbe91dd91996ba462a134a0d14e03ef4c3d572313bb4b49d86" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# List Comprehension 2\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:** Based on http://datasciencelab.wordpress.com/2014/01/08/list-comprehension-in-python/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## First Letter Of Every Word" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Import the string module" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import string" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 6 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a variable that includes all punctuation and adds a blank space\n", "\n", "string.punctuation is: !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" ] }, { "cell_type": "code", "collapsed": false, "input": [ "punct = string.punctuation + ' '" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 7 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a string for all vowels" ] }, { "cell_type": "code", "collapsed": false, "input": [ "vowels = 'aeiou'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 8 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create some text" ] }, { "cell_type": "code", "collapsed": false, "input": [ "phrase = 'Strictly speaking Python strings are categorized as immutable sequences'" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 9 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a variable for the first letter in every word of phase, after split up" ] }, { "cell_type": "code", "collapsed": false, "input": [ "first_letter = [w[0] for w in phrase.split()]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 10 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Print it out" ] }, { "cell_type": "code", "collapsed": false, "input": [ "first_letter" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 11, "text": [ "['S', 's', 'P', 's', 'a', 'c', 'a', 'i', 's']" ] } ], "prompt_number": 11 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### In the above example here is the breakdown of the list Comprehension\n", "\n", "- [w[0] is the output. That is, what we want done.\n", "- for w in is the iterator identifier\n", "- phrase.split()] is the input" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### You can also add a conditional statement" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create some simulated numbers" ] }, { "cell_type": "code", "collapsed": false, "input": [ "numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 12 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Create a variable called output that squared every element in numbers variable if it is divisible by 2" ] }, { "cell_type": "code", "collapsed": false, "input": [ "output = [x**2 for x in numbers if x%2 == 0]" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "markdown", "metadata": {}, "source": [ "### View the output" ] }, { "cell_type": "code", "collapsed": false, "input": [ "output" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 14, "text": [ "[0, 4, 16, 36, 64, 100]" ] } ], "prompt_number": 14 } ], "metadata": {} } ] }