2015-01-25 19:52:00 +08:00
{
"metadata": {
"name": "",
2015-01-27 23:24:42 +08:00
"signature": "sha256:8ee2873e1a28218e63482afb0f83c35bf5a94310ceb69ce5945a5b710720ecb8"
2015-01-25 19:52:00 +08:00
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Data Structures"
]
},
2015-01-26 20:27:42 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"* tuple\n",
"* list\n",
2015-01-26 20:35:41 +08:00
"* dict\n",
"* set"
2015-01-26 20:27:42 +08:00
]
},
2015-01-25 19:52:00 +08:00
{
"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": [
2015-01-27 23:24:42 +08:00
"A tuple is a one dimensional, fixed-length, immutable sequence.\n",
"\n",
"Create a tuple:"
2015-01-25 22:34:33 +08:00
]
},
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
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Convert to a tuple:"
2015-01-25 22:34:33 +08:00
]
},
2015-01-25 19:52:00 +08:00
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-27 23:24:42 +08:00
"list_1 = [1, 2, 3]\n",
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-27 23:24:42 +08:00
"prompt_number": 2,
2015-01-25 19:52:00 +08:00
"text": [
"tuple"
]
}
],
2015-01-27 23:24:42 +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": [
2015-01-27 23:24:42 +08:00
"Create a nested tuple:"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 3,
2015-01-25 19:52:00 +08:00
"text": [
"([1, 2, 3], (4, 5))"
]
}
],
2015-01-27 23:24:42 +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": [
2015-01-27 23:24:42 +08:00
"Access a tuple's elements by index O(1):"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 4,
2015-01-25 19:52:00 +08:00
"text": [
"[1, 2, 3]"
]
}
],
2015-01-27 23:24:42 +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": [
2015-01-27 23:24:42 +08:00
"Although tuples are immutable, their contents can contain mutable objects. \n",
"\n",
"Modify a tuple's contents:"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 5,
2015-01-25 19:52:00 +08:00
"text": [
"[1, 2, 3, 4]"
]
}
],
2015-01-27 23:24:42 +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": [
2015-01-27 23:24:42 +08:00
"Concatenate tuples by creating a new tuple and copying objects:"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 6,
2015-01-25 19:52:00 +08:00
"text": [
"(1, 3, 2, 4, 5, 6)"
]
}
],
2015-01-27 23:24:42 +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": [
2015-01-27 23:24:42 +08:00
"Multiply tuples to copy references to objects (objects themselves are not copied):"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 7,
2015-01-25 19:52:00 +08:00
"text": [
"('foo', 'bar', 'foo', 'bar')"
]
}
],
2015-01-27 23:24:42 +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": [
2015-01-27 23:24:42 +08:00
"Unpack tuples:"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 8,
2015-01-25 19:52:00 +08:00
"text": [
"([1, 2, 3, 4], (4, 5))"
]
}
],
2015-01-27 23:24:42 +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": [
2015-01-27 23:24:42 +08:00
"Unpack nested tuples:"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 9,
2015-01-25 19:52:00 +08:00
"text": [
"(1, 2, 3, 4, 4, 5)"
]
}
],
2015-01-27 23:24:42 +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": [
2015-01-27 23:24:42 +08:00
"A common use of variable unpacking is when iterating over sequences of tuples or lists:"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 10
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": [
2015-01-27 23:24:42 +08:00
"A list is a one dimensional, variable-length, mutable sequence.\n",
"\n",
"Create a list:"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 11,
2015-01-25 20:24:12 +08:00
"text": [
"[1, 2, 3]"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 11
2015-01-25 20:24:12 +08:00
},
2015-01-25 22:34:33 +08:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Convert to a list:"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 12,
2015-01-25 20:24:12 +08:00
"text": [
"list"
]
}
],
2015-01-27 23:24:42 +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": [
2015-01-27 23:24:42 +08:00
"Create a nested list:"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 13,
2015-01-25 20:24:12 +08:00
"text": [
"[(1, 2, 3), [4, 5]]"
]
}
],
2015-01-27 23:24:42 +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": [
2015-01-27 23:24:42 +08:00
"Access a list's elements by index O(1):"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 14,
2015-01-25 20:24:12 +08:00
"text": [
"[4, 5]"
]
}
],
2015-01-27 23:24:42 +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": [
2015-01-27 23:24:42 +08:00
"Append an element to a list O(1):"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 15,
2015-01-25 20:24:12 +08:00
"text": [
"[(1, 2, 3), [4, 5], 6]"
]
}
],
2015-01-27 23:24:42 +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": [
2015-01-27 23:24:42 +08:00
"Insert an element to a list at a specific index (note that insert is expensive as it has to shift subsequent elements O(n)):"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 16,
2015-01-25 20:24:12 +08:00
"text": [
"['start', (1, 2, 3), [4, 5], 6]"
]
}
],
2015-01-27 23:24:42 +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": [
2015-01-27 23:24:42 +08:00
"Pop is expensive as it has to shift subsequent elements O(n). The operation is O(1) if pop is used for the last element.\n",
"\n",
"Remove and return an element from a specified index:"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 17,
2015-01-25 20:24:12 +08:00
"text": [
"[(1, 2, 3), [4, 5], 6]"
]
}
],
2015-01-27 23:24:42 +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": [
2015-01-27 23:24:42 +08:00
"Locates the first such value and remove it O(n):"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 18,
2015-01-25 20:24:12 +08:00
"text": [
"[[4, 5], 6]"
]
}
],
2015-01-27 23:24:42 +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": [
2015-01-27 23:24:42 +08:00
"Check if a list contains a value O(n):"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 19,
2015-01-25 20:24:12 +08:00
"text": [
"True"
]
}
],
2015-01-27 23:24:42 +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": [
2015-01-27 23:24:42 +08:00
"Concatenate lists by creating a new list and copying objects:"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 20,
2015-01-25 20:24:12 +08:00
"text": [
"[1, 3, 2, 4, 5, 6]"
]
}
],
2015-01-27 23:24:42 +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": [
2015-01-27 23:24:42 +08:00
"Extend a list by appending elements (faster than concatenating lists, as it does not have to create a new list):"
2015-01-25 22:34:33 +08:00
]
},
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-27 23:24:42 +08:00
"prompt_number": 21,
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-27 23:24:42 +08:00
"prompt_number": 21
2015-01-26 05:09:07 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## dict"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"A dict is also known as a hash map or associative array. A dict is a mutable collection of key-value pairs.\n",
"\n",
"Note: Big O complexities are listed as average case, with most worst case complexities being O(n).\n",
2015-01-26 05:09:07 +08:00
"\n",
2015-01-27 23:24:42 +08:00
"Create a dict:"
2015-01-26 05:09:07 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dict_1 = { 'a' : 'foo', 'b' : [0, 1, 2, 3] }\n",
"dict_1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 22,
2015-01-26 05:09:07 +08:00
"text": [
"{'a': 'foo', 'b': [0, 1, 2, 3]}"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 22
2015-01-26 05:09:07 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Access a dict's elements by index O(1)"
2015-01-26 05:09:07 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dict_1['b']"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 23,
2015-01-26 05:09:07 +08:00
"text": [
"[0, 1, 2, 3]"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 23
2015-01-26 05:09:07 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Insert or set a dict's elements by index O(1):"
2015-01-26 05:09:07 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dict_1[5] = 'bar'\n",
"dict_1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 24,
2015-01-26 05:09:07 +08:00
"text": [
"{5: 'bar', 'a': 'foo', 'b': [0, 1, 2, 3]}"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 24
2015-01-26 05:09:07 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Check if a dict contains a key O(1):"
2015-01-26 05:09:07 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"5 in dict_1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 25,
2015-01-26 05:09:07 +08:00
"text": [
"True"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 25
2015-01-26 05:09:07 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Delete a value from a dict O(1):"
2015-01-26 05:09:07 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dict_2 = dict(dict_1)\n",
"del dict_2[5]\n",
"dict_2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 26,
2015-01-26 05:09:07 +08:00
"text": [
"{'a': 'foo', 'b': [0, 1, 2, 3]}"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 26
2015-01-26 05:09:07 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Remove and return an element from a specified index O(1):"
2015-01-26 05:09:07 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"value = dict_2.pop('b')\n",
"print(value)\n",
"print(dict_2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[0, 1, 2, 3]\n",
"{'a': 'foo'}\n"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 27
2015-01-26 05:09:07 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Get or pop can be called with a default value if the key is not found. By default, get() will return None and pop() will throw an exception if the key is not found."
2015-01-26 05:09:07 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"value = dict_1.get('z', 0)\n",
"value"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 28,
2015-01-26 05:09:07 +08:00
"text": [
"0"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 28
2015-01-26 05:09:07 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Return a default value if the key is not found:"
2015-01-26 05:09:07 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(dict_1.setdefault('b', None))\n",
"print(dict_1.setdefault('z', None))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[0, 1, 2, 3]\n",
"None\n"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 29
2015-01-26 05:09:07 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"By contrast to setdefault(), defaultdict lets you specify the default when the container is initialized, which works well if the default is appropriate for all keys:"
2015-01-26 05:09:07 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from collections import defaultdict\n",
"\n",
"seq = ['foo', 'bar', 'baz']\n",
"first_letter = defaultdict(list)\n",
"for elem in seq:\n",
" first_letter[elem[0]].append(elem)\n",
"first_letter"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 30,
2015-01-26 05:09:07 +08:00
"text": [
"defaultdict(<type 'list'>, {'b': ['bar', 'baz'], 'f': ['foo']})"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 30
2015-01-26 05:09:07 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"dict keys must be \"hashable\", i.e. they must be immutable objects like scalars (int, float, string) or tuples whose objects are all immutable. Lists are mutable and therefore are not hashable, although you can convert the list portion to a tuple as a quick fix."
2015-01-26 05:09:07 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print(hash('string'))\n",
"print(hash((1, 2, (3, 4))))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"-9167918882415130555\n",
"-2725224101759650258\n"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 31
2015-01-26 05:09:07 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 01:58:58 +08:00
"Get the list of keys in no particular order (although keys() outputs the keys in the same order). In Python 3, keys() returns an iterator instead of a list."
2015-01-26 05:09:07 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-27 01:58:58 +08:00
"dict_1.keys()"
2015-01-26 05:09:07 +08:00
],
"language": "python",
"metadata": {},
"outputs": [
{
2015-01-27 01:58:58 +08:00
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 32,
2015-01-27 01:58:58 +08:00
"text": [
"['a', 'b', 5, 'z']"
2015-01-26 05:09:07 +08:00
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 32
2015-01-26 05:09:07 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 01:58:58 +08:00
"Get the list of values in no particular order (although values() outputs the keys in the same order). In Python 3, keys() returns an iterator instead of a list."
2015-01-26 05:09:07 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-27 01:58:58 +08:00
"dict_1.values()"
2015-01-26 05:09:07 +08:00
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 33,
2015-01-26 05:09:07 +08:00
"text": [
2015-01-27 01:58:58 +08:00
"['foo', [0, 1, 2, 3], 'bar', None]"
2015-01-26 05:09:07 +08:00
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 33
2015-01-26 05:09:07 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Iterate through a dictionary's keys and values:"
2015-01-26 05:09:07 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2015-01-27 01:58:58 +08:00
"for key, value in dict_1.items():\n",
" print key, value"
2015-01-26 05:09:07 +08:00
],
"language": "python",
"metadata": {},
"outputs": [
{
2015-01-27 01:58:58 +08:00
"output_type": "stream",
"stream": "stdout",
2015-01-26 05:09:07 +08:00
"text": [
2015-01-27 01:58:58 +08:00
"a foo\n",
"b [0, 1, 2, 3]\n",
"5 bar\n",
"z None\n"
2015-01-26 05:09:07 +08:00
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 34
2015-01-26 05:09:07 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Merge one dict into another:"
2015-01-26 05:09:07 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"dict_1.update({'e' : 'elephant', 'f' : 'fish'})\n",
"dict_1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 35,
2015-01-26 05:09:07 +08:00
"text": [
"{5: 'bar',\n",
" 'a': 'foo',\n",
" 'b': [0, 1, 2, 3],\n",
" 'e': 'elephant',\n",
" 'f': 'fish',\n",
" 'z': None}"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 35
2015-01-26 05:09:07 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Pair up two sequences element-wise in a dict:"
2015-01-26 05:09:07 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mapping = dict(zip(range(7), reversed(range(7))))\n",
"mapping"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 36,
2015-01-26 05:09:07 +08:00
"text": [
"{0: 6, 1: 5, 2: 4, 3: 3, 4: 2, 5: 1, 6: 0}"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 36
2015-01-26 20:32:04 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## set"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"A set is an unordered sequence of unique elements. \n",
"\n",
"Create a set:"
2015-01-26 20:32:04 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"set_1 = set([0, 1, 2, 3, 4, 5])\n",
"set_1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 37,
2015-01-26 20:32:04 +08:00
"text": [
"{0, 1, 2, 3, 4, 5}"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 37
2015-01-26 20:32:04 +08:00
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"set_2 = {1, 2, 3, 5, 8, 13}\n",
"set_2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 38,
2015-01-26 20:32:04 +08:00
"text": [
"{1, 2, 3, 5, 8, 13}"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 38
2015-01-26 20:32:04 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Sets support set operations like union, intersection, difference, and symmetric difference."
2015-01-26 20:32:04 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Union O(len(set_1) + len(set_2)):"
2015-01-26 20:32:04 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"set_1 | set_2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 39,
2015-01-26 20:32:04 +08:00
"text": [
"{0, 1, 2, 3, 4, 5, 8, 13}"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 39
2015-01-26 20:32:04 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Intersection O(min(len(set_1), len(set_2)):"
2015-01-26 20:32:04 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"set_1 & set_2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 40,
2015-01-26 20:32:04 +08:00
"text": [
"{1, 2, 3, 5}"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 40
2015-01-26 20:32:04 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Difference O(len(set_1)):"
2015-01-26 20:32:04 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"set_1 - set_2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 41,
2015-01-26 20:32:04 +08:00
"text": [
"{0, 4}"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 41
2015-01-26 20:32:04 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Symmetric Difference O(len(set_1)):"
2015-01-26 20:32:04 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"set_1 ^ set_2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 42,
2015-01-26 20:32:04 +08:00
"text": [
"{0, 4, 8, 13}"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 42
2015-01-26 20:32:04 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Subset O(len(set_3)):"
2015-01-26 20:32:04 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"set_3 = {1, 2, 3}\n",
"set_3.issubset(set_2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 43,
2015-01-26 20:32:04 +08:00
"text": [
"True"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 43
2015-01-26 20:32:04 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Superset O(len(set_3)):"
2015-01-26 20:32:04 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"set_2.issuperset(set_3)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 44,
2015-01-26 20:32:04 +08:00
"text": [
"True"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 44
2015-01-26 20:32:04 +08:00
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-27 23:24:42 +08:00
"Equal O(min(len(set_1), len(set_2)):"
2015-01-26 20:32:04 +08:00
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"{1, 2, 3} == {3, 2, 1}"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-27 23:24:42 +08:00
"prompt_number": 45,
2015-01-26 20:32:04 +08:00
"text": [
"True"
]
}
],
2015-01-27 23:24:42 +08:00
"prompt_number": 45
2015-01-25 19:52:00 +08:00
}
],
"metadata": {}
}
]
}