2015-01-25 19:52:00 +08:00
{
"metadata": {
"name": "",
2015-01-26 01:55:43 +08:00
"signature": "sha256:c5099245c74de456735197b7914b9a407b7fd42bc3fd7db0d637d1423436d0e6"
2015-01-25 19:52:00 +08:00
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Data Structures"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-25 21:18:02 +08:00
"## tuple"
2015-01-25 19:52:00 +08:00
]
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"One dimensional, fixed-length, immutable sequence"
]
},
2015-01-25 19:52:00 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"tup = (1, 2, 3)\n",
"tup"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 1,
2015-01-25 19:52:00 +08:00
"text": [
"(1, 2, 3)"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 1
2015-01-25 19:52:00 +08:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-26 01:41:04 +08:00
"list_1 = [1, 2, 3]"
2015-01-25 19:52:00 +08:00
],
"language": "python",
"metadata": {},
"outputs": [],
2015-01-25 21:18:02 +08:00
"prompt_number": 2
2015-01-25 19:52:00 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Convert to a tuple"
]
},
2015-01-25 19:52:00 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-26 01:41:04 +08:00
"type(tuple(list_1))"
2015-01-25 19:52:00 +08:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 3,
2015-01-25 19:52:00 +08:00
"text": [
"tuple"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 3
2015-01-25 19:52:00 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nested tuples"
]
},
2015-01-25 19:52:00 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"nested_tup = ([1, 2, 3], (4, 5))\n",
"nested_tup"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 4,
2015-01-25 19:52:00 +08:00
"text": [
"([1, 2, 3], (4, 5))"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 4
2015-01-25 19:52:00 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Access by index O(1)"
]
},
2015-01-25 19:52:00 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"nested_tup[0]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 5,
2015-01-25 19:52:00 +08:00
"text": [
"[1, 2, 3]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 5
2015-01-25 19:52:00 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Although tuples are immutable, their contents can contain mutable objects"
]
},
2015-01-25 19:52:00 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"nested_tup[0].append(4)\n",
"nested_tup[0]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 6,
2015-01-25 19:52:00 +08:00
"text": [
"[1, 2, 3, 4]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 6
2015-01-25 19:52:00 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Concatenate tuples by creating a new tuple and copying objects"
]
},
2015-01-25 19:52:00 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"(1, 3, 2) + (4, 5, 6)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 7,
2015-01-25 19:52:00 +08:00
"text": [
"(1, 3, 2, 4, 5, 6)"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 7
2015-01-25 19:52:00 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Multiply copies references to objects (objects themselves are not copied)"
]
},
2015-01-25 19:52:00 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"('foo', 'bar') * 2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 8,
2015-01-25 19:52:00 +08:00
"text": [
"('foo', 'bar', 'foo', 'bar')"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 8
2015-01-25 19:52:00 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Unpack tuples"
]
},
2015-01-25 19:52:00 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"a, b = nested_tup\n",
"a, b"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 9,
2015-01-25 19:52:00 +08:00
"text": [
"([1, 2, 3, 4], (4, 5))"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 9
2015-01-25 19:52:00 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Unpack nested tuples"
]
},
2015-01-25 19:52:00 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"(a, b, c, d), (e, f) = nested_tup\n",
"a, b, c, d, e, f"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 10,
2015-01-25 19:52:00 +08:00
"text": [
"(1, 2, 3, 4, 4, 5)"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 10
2015-01-25 19:52:00 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A common use of variable unpacking is when iterating over sequences of tuples or lists"
]
},
2015-01-25 19:52:00 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"seq = [( 1, 2, 3), (4, 5, 6), (7, 8, 9)] \n",
"for a, b, c in seq: \n",
" print(a, b, c)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"(1, 2, 3)\n",
"(4, 5, 6)\n",
"(7, 8, 9)\n"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 11
2015-01-25 20:24:12 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-25 21:18:02 +08:00
"## list"
2015-01-25 20:24:12 +08:00
]
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"One dimensional, variable-length, mutable sequence"
]
},
2015-01-25 20:24:12 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-26 01:41:04 +08:00
"list_1 = [1, 2, 3]\n",
"list_1"
2015-01-25 20:24:12 +08:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-26 01:55:43 +08:00
"prompt_number": 12,
2015-01-25 20:24:12 +08:00
"text": [
"[1, 2, 3]"
]
}
],
2015-01-26 01:55:43 +08:00
"prompt_number": 12
2015-01-25 20:24:12 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Convert to a list"
]
},
2015-01-25 20:24:12 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"type(list(tup))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 13,
2015-01-25 20:24:12 +08:00
"text": [
"list"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 13
2015-01-25 20:24:12 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nested list"
]
},
2015-01-25 20:24:12 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"nested_list = [(1, 2, 3), [4, 5]]\n",
"nested_list"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 14,
2015-01-25 20:24:12 +08:00
"text": [
"[(1, 2, 3), [4, 5]]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 14
2015-01-25 20:24:12 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Access by index"
]
},
2015-01-25 20:24:12 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"nested_list[1]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 15,
2015-01-25 20:24:12 +08:00
"text": [
"[4, 5]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 15
2015-01-25 20:24:12 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Append an element O(1)"
]
},
2015-01-25 20:24:12 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"nested_list.append(6)\n",
"nested_list"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 16,
2015-01-25 20:24:12 +08:00
"text": [
"[(1, 2, 3), [4, 5], 6]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 16
2015-01-25 20:24:12 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Insert an element at a specific index. Insert is expensive as it has to shift subsequent elements O(n)."
]
},
2015-01-25 20:24:12 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"nested_list.insert(0, 'start')\n",
"nested_list"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 17,
2015-01-25 20:24:12 +08:00
"text": [
"['start', (1, 2, 3), [4, 5], 6]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 17
2015-01-25 20:24:12 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pop removes and returns an element from a specified index. Pop is expensive as it has to shift subsequent elements O(n). O(1) if pop is used for the last element"
]
},
2015-01-25 20:24:12 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"nested_list.pop(0)\n",
"nested_list"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 18,
2015-01-25 20:24:12 +08:00
"text": [
"[(1, 2, 3), [4, 5], 6]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 18
2015-01-25 20:24:12 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Remove locates the first such value and removes it O(n)"
]
},
2015-01-25 20:24:12 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"nested_list.remove((1, 2, 3))\n",
"nested_list"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 19,
2015-01-25 20:24:12 +08:00
"text": [
"[[4, 5], 6]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 19
2015-01-25 20:24:12 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Check if a list contains a value O(n)"
]
},
2015-01-25 20:24:12 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"6 in nested_list"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 20,
2015-01-25 20:24:12 +08:00
"text": [
"True"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 20
2015-01-25 20:24:12 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Concatenate lists by creating a new list and copying objects"
]
},
2015-01-25 20:24:12 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"[1, 3, 2] + [4, 5, 6]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 21,
2015-01-25 20:24:12 +08:00
"text": [
"[1, 3, 2, 4, 5, 6]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 21
2015-01-25 20:24:12 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Extend a list by appending elements. Faster than concatenating lists."
]
},
2015-01-25 20:24:12 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
"nested_list.extend([7, 8, 9])\n",
"nested_list"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 22,
2015-01-25 20:24:12 +08:00
"text": [
2015-01-25 21:18:02 +08:00
"[[4, 5], 6, 7, 8, 9]"
2015-01-25 20:24:12 +08:00
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 22
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## sort"
]
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sort in-place O(n log n)"
]
},
2015-01-25 21:18:02 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-26 01:41:04 +08:00
"seq = [1, 5, 3, 9, 7, 6]\n",
"seq.sort()\n",
"seq"
2015-01-25 21:18:02 +08:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:25:44 +08:00
"prompt_number": 23,
2015-01-25 21:18:02 +08:00
"text": [
"[1, 3, 5, 6, 7, 9]"
]
}
],
2015-01-25 21:25:44 +08:00
"prompt_number": 23
2015-01-25 21:18:02 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sort by secondary key: str length"
]
},
2015-01-25 21:18:02 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-26 01:41:04 +08:00
"seq = ['the', 'quick', 'brown', 'fox', 'jumps', 'over']\n",
"seq.sort(key=len)\n",
"seq"
2015-01-25 21:18:02 +08:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:25:44 +08:00
"prompt_number": 24,
2015-01-25 21:18:02 +08:00
"text": [
"['the', 'fox', 'over', 'quick', 'brown', 'jumps']"
]
}
],
2015-01-25 21:25:44 +08:00
"prompt_number": 24
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## bisect"
]
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The bisect module does not check whether the list is sorted, as this check would be expensive O(n). Using bisect on an unsorted list will not result in an error but could lead to incorrect results."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import bisect"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 25
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Find the location where an element should be inserted to keep the list sorted"
]
},
2015-01-25 21:25:44 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-26 01:41:04 +08:00
"seq = [1, 2, 2, 3, 5, 13]\n",
"bisect.bisect(seq, 8)"
2015-01-25 21:25:44 +08:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 22:34:33 +08:00
"prompt_number": 26,
2015-01-25 21:25:44 +08:00
"text": [
2015-01-25 22:04:56 +08:00
"5"
2015-01-25 21:25:44 +08:00
]
}
],
2015-01-25 22:34:33 +08:00
"prompt_number": 26
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Inserts an element into a location to keep the list sorted"
]
2015-01-25 21:25:44 +08:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-26 01:41:04 +08:00
"bisect.insort(seq, 8)\n",
"seq"
2015-01-25 21:25:44 +08:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 22:34:33 +08:00
"prompt_number": 27,
2015-01-25 21:25:44 +08:00
"text": [
2015-01-25 22:04:56 +08:00
"[1, 2, 2, 3, 5, 8, 13]"
2015-01-25 21:25:44 +08:00
]
}
],
2015-01-25 22:34:33 +08:00
"prompt_number": 27
2015-01-25 22:04:56 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## slice"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![alt text](http://www.nltk.org/images/string-slicing.png)"
]
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Select a section of list types (arrays, tuples, NumPy arrays) using [start:stop]. start is included, stop is not. The number of elements in the result is stop - start."
]
},
2015-01-25 22:04:56 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-26 01:41:04 +08:00
"seq = 'Monty Python'\n",
"seq[6:10]"
2015-01-25 22:04:56 +08:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 22:34:33 +08:00
"prompt_number": 28,
2015-01-25 22:04:56 +08:00
"text": [
"'Pyth'"
]
}
],
2015-01-25 22:34:33 +08:00
"prompt_number": 28
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Omit start to default to start of the sequence"
]
2015-01-25 22:04:56 +08:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-26 01:41:04 +08:00
"seq[:5]"
2015-01-25 22:04:56 +08:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 22:34:33 +08:00
"prompt_number": 29,
2015-01-25 22:04:56 +08:00
"text": [
"'Monty'"
]
}
],
2015-01-25 22:34:33 +08:00
"prompt_number": 29
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Omit end to default to end of the sequence"
]
2015-01-25 22:04:56 +08:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-26 01:41:04 +08:00
"seq[6:]"
2015-01-25 22:04:56 +08:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 22:34:33 +08:00
"prompt_number": 30,
2015-01-25 22:04:56 +08:00
"text": [
"'Python'"
]
}
],
2015-01-25 22:34:33 +08:00
"prompt_number": 30
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Negative indices slice relative to the end"
]
2015-01-25 22:04:56 +08:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-26 01:41:04 +08:00
"seq[-12:-7]"
2015-01-25 22:04:56 +08:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 22:34:33 +08:00
"prompt_number": 31,
2015-01-25 22:04:56 +08:00
"text": [
"'Monty'"
]
}
],
2015-01-25 22:34:33 +08:00
"prompt_number": 31
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Slice can also take a step such as the one below, which takes every other element"
]
2015-01-25 22:04:56 +08:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-26 01:41:04 +08:00
"seq[::2]"
2015-01-25 22:04:56 +08:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 22:34:33 +08:00
"prompt_number": 32,
2015-01-25 22:04:56 +08:00
"text": [
2015-01-25 22:34:33 +08:00
"'MnyPto'"
2015-01-25 22:04:56 +08:00
]
}
],
2015-01-25 22:34:33 +08:00
"prompt_number": 32
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Passing -1 for the step reverses the list or tuple:"
]
2015-01-25 22:04:56 +08:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-26 01:41:04 +08:00
"seq[::-1]"
2015-01-25 22:04:56 +08:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 22:34:33 +08:00
"prompt_number": 33,
2015-01-25 22:04:56 +08:00
"text": [
2015-01-25 22:34:33 +08:00
"'nohtyP ytnoM'"
2015-01-25 22:04:56 +08:00
]
}
],
2015-01-25 22:34:33 +08:00
"prompt_number": 33
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Assign elements to a slice. Slice range does not have to equal number of elements to assign."
]
2015-01-25 22:04:56 +08:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-26 01:41:04 +08:00
"seq = [1, 1, 2, 3, 5, 8, 13]\n",
"seq[5:] = ['H', 'a', 'l', 'l']\n",
"seq"
2015-01-25 22:04:56 +08:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 22:34:33 +08:00
"prompt_number": 34,
2015-01-25 22:04:56 +08:00
"text": [
"[1, 1, 2, 3, 5, 'H', 'a', 'l', 'l']"
]
}
],
2015-01-25 22:34:33 +08:00
"prompt_number": 34
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Compare assigning into a slice (above) versus assigning into an index (below)"
]
2015-01-25 22:04:56 +08:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-26 01:41:04 +08:00
"seq = [1, 1, 2, 3, 5, 8, 13]\n",
"seq[5] = ['H', 'a', 'l', 'l']\n",
"seq"
2015-01-25 22:04:56 +08:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 22:34:33 +08:00
"prompt_number": 35,
2015-01-25 22:04:56 +08:00
"text": [
"[1, 1, 2, 3, 5, ['H', 'a', 'l', 'l'], 13]"
]
}
],
2015-01-25 22:34:33 +08:00
"prompt_number": 35
2015-01-26 01:18:28 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## sorted"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Return a new sorted list from the elements of a sequence"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sorted([2, 5, 1, 8, 7, 9])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 36,
"text": [
"[1, 2, 5, 7, 8, 9]"
]
}
],
"prompt_number": 36
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sorted('foo bar baz')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 37,
"text": [
"[' ', ' ', 'a', 'a', 'b', 'b', 'f', 'o', 'o', 'r', 'z']"
]
}
],
"prompt_number": 37
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It's common to get a sorted list of unique elements by combining sorted and set"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"seq = [2, 5, 1, 8, 7, 9, 9, 2, 5, 1, (4, 2), (1, 2), (1, 2)]\n",
"sorted(set(seq))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-26 01:55:43 +08:00
"prompt_number": 38,
2015-01-26 01:18:28 +08:00
"text": [
"[1, 2, 5, 7, 8, 9, (1, 2), (4, 2)]"
]
}
],
2015-01-26 01:55:43 +08:00
"prompt_number": 38
2015-01-26 01:44:19 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## reversed"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Iterates over the sequence elements in reverse order"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"list(reversed(seq))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-26 01:55:43 +08:00
"prompt_number": 39,
2015-01-26 01:44:19 +08:00
"text": [
"[(1, 2), (1, 2), (4, 2), 1, 5, 2, 9, 9, 7, 8, 1, 5, 2]"
]
}
],
2015-01-26 01:55:43 +08:00
"prompt_number": 39
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## enumerate"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"enumerate gives you the index of a collection and the value"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"numbers = ['zero', 'one', 'two', 'three']\n",
"for i, number in enumerate(numbers):\n",
" print(i, number)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"(0, 'zero')\n",
"(1, 'one')\n",
"(2, 'two')\n",
"(3, 'three')\n"
]
}
],
"prompt_number": 40
2015-01-26 01:41:04 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## zip"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pairs up the elements of sequences to create a list of tuples"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"seq_1 = [1, 2, 3]\n",
"seq_2 = ['foo', 'bar', 'baz']\n",
"zip(seq_1, seq_2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 41,
"text": [
"[(1, 'foo'), (2, 'bar'), (3, 'baz')]"
]
}
],
"prompt_number": 41
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Takes an arbitrary number of sequences. The number of elements it produces is determined by the shortest sequence."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"seq_3 = [True, False]\n",
"zip(seq_1, seq_2, seq_3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 42,
"text": [
"[(1, 'foo', True), (2, 'bar', False)]"
]
}
],
"prompt_number": 42
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is common to use zip for simultaneously iterating over multiple sequences combined with enumerate"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for i, (a, b) in enumerate(zip(seq_1, seq_2)):\n",
" print('%d: %s, %s' % (i, a, b))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0: 1, foo\n",
"1: 2, bar\n",
"2: 3, baz\n"
]
}
],
2015-01-26 01:55:43 +08:00
"prompt_number": 43
2015-01-26 01:41:04 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"zip can unzip a zipped sequence, which you can think of as converting a list of rows into a list of columns"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"numbers = [(1, 'one'), (2, 'two'), (3, 'three')]\n",
"a, b = zip(*numbers)\n",
"a"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-26 01:55:43 +08:00
"prompt_number": 44,
2015-01-26 01:41:04 +08:00
"text": [
"(1, 2, 3)"
]
}
],
2015-01-26 01:55:43 +08:00
"prompt_number": 44
2015-01-26 01:41:04 +08:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"b"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-26 01:55:43 +08:00
"prompt_number": 45,
2015-01-26 01:41:04 +08:00
"text": [
"('one', 'two', 'three')"
]
}
],
2015-01-26 01:55:43 +08:00
"prompt_number": 45
2015-01-25 19:52:00 +08:00
}
],
"metadata": {}
}
]
}