Added comprehension snippets for list, dict, and set. Tweaked a few snippets and content.

This commit is contained in:
Donne Martin 2015-01-26 13:00:12 -05:00
parent c8a06dcd85
commit bf1f35d7af

View File

@ -1,7 +1,7 @@
{ {
"metadata": { "metadata": {
"name": "", "name": "",
"signature": "sha256:73ff6384d9af604860ac45769daf4ddad270f251d7838e691c99d5d419e0c72b" "signature": "sha256:893799227c6632abab6f36b3299512e1f724d3a9e69a9cc0b81a3ff9c872f370"
}, },
"nbformat": 3, "nbformat": 3,
"nbformat_minor": 0, "nbformat_minor": 0,
@ -12,7 +12,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# Data Structures Utilities" "# Data Structure Utilities"
] ]
}, },
{ {
@ -26,7 +26,8 @@
"* sorted\n", "* sorted\n",
"* reversed\n", "* reversed\n",
"* enumerate\n", "* enumerate\n",
"* zip" "* zip\n",
"* list comprehensions"
] ]
}, },
{ {
@ -666,9 +667,9 @@
"cell_type": "code", "cell_type": "code",
"collapsed": false, "collapsed": false,
"input": [ "input": [
"numbers = ['zero', 'one', 'two', 'three']\n", "strings = ['foo', 'bar', 'baz']\n",
"for i, number in enumerate(numbers):\n", "for i, string in enumerate(strings):\n",
" print(i, number)" " print(i, string)"
], ],
"language": "python", "language": "python",
"metadata": {}, "metadata": {},
@ -677,10 +678,9 @@
"output_type": "stream", "output_type": "stream",
"stream": "stdout", "stream": "stdout",
"text": [ "text": [
"(0, 'zero')\n", "(0, 'foo')\n",
"(1, 'one')\n", "(1, 'bar')\n",
"(2, 'two')\n", "(2, 'baz')\n"
"(3, 'three')\n"
] ]
} }
], ],
@ -827,6 +827,154 @@
} }
], ],
"prompt_number": 27 "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": {} "metadata": {}