From bf1f35d7af9a0f7eb735407ad3b934c234fb4555 Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Mon, 26 Jan 2015 13:00:12 -0500 Subject: [PATCH] Added comprehension snippets for list, dict, and set. Tweaked a few snippets and content. --- core/structs_utils.ipynb | 168 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 158 insertions(+), 10 deletions(-) diff --git a/core/structs_utils.ipynb b/core/structs_utils.ipynb index d4979d7..227f002 100644 --- a/core/structs_utils.ipynb +++ b/core/structs_utils.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:73ff6384d9af604860ac45769daf4ddad270f251d7838e691c99d5d419e0c72b" + "signature": "sha256:893799227c6632abab6f36b3299512e1f724d3a9e69a9cc0b81a3ff9c872f370" }, "nbformat": 3, "nbformat_minor": 0, @@ -12,7 +12,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Data Structures Utilities" + "# Data Structure Utilities" ] }, { @@ -26,7 +26,8 @@ "* sorted\n", "* reversed\n", "* enumerate\n", - "* zip" + "* zip\n", + "* list comprehensions" ] }, { @@ -666,9 +667,9 @@ "cell_type": "code", "collapsed": false, "input": [ - "numbers = ['zero', 'one', 'two', 'three']\n", - "for i, number in enumerate(numbers):\n", - " print(i, number)" + "strings = ['foo', 'bar', 'baz']\n", + "for i, string in enumerate(strings):\n", + " print(i, string)" ], "language": "python", "metadata": {}, @@ -677,10 +678,9 @@ "output_type": "stream", "stream": "stdout", "text": [ - "(0, 'zero')\n", - "(1, 'one')\n", - "(2, 'two')\n", - "(3, 'three')\n" + "(0, 'foo')\n", + "(1, 'bar')\n", + "(2, 'baz')\n" ] } ], @@ -827,6 +827,154 @@ } ], "prompt_number": 27 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## List Comprehensions" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Concisely form a new list by filtering the elements of a sequence and transforming the elements passing the filter. List comprehensions take the form:\n", + "```python\n", + "[expr for val in collection if condition]\n", + "```\n", + "Which is equivalent to the following for loop:\n", + "```python\n", + "result = []\n", + "for val in collection:\n", + " if condition:\n", + " result.append(expr)\n", + "```" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Convert to upper case all strings that start with a 'b'" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "strings = ['foo', 'bar', 'baz', 'f', 'fo', 'b', 'ba']\n", + "[x.upper() for x in strings if x[0] == 'b']" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 28, + "text": [ + "['BAR', 'BAZ', 'B', 'BA']" + ] + } + ], + "prompt_number": 28 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "List comprehensions can be nested" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "list_of_tuples = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]\n", + "[x for tup in list_of_tuples for x in tup]" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 29, + "text": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9]" + ] + } + ], + "prompt_number": 29 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Dict Comprehension" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Create a lookup map of strings and their locations in the list for strings that start with a 'b'" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "{index : val for index, val in enumerate(strings) if val[0] == 'b'}" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 30, + "text": [ + "{1: 'bar', 2: 'baz', 5: 'b', 6: 'ba'}" + ] + } + ], + "prompt_number": 30 + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Set Comprehension" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Get the unique lengths of strings that start with a 'b'" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "{len(x) for x in strings if x[0] == 'b'}" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "metadata": {}, + "output_type": "pyout", + "prompt_number": 31, + "text": [ + "{1, 2, 3}" + ] + } + ], + "prompt_number": 31 } ], "metadata": {}