data-science-ipython-notebooks/core/structs.ipynb

629 lines
12 KiB
Plaintext
Raw Normal View History

{
"metadata": {
"name": "",
"signature": "sha256:cb47861bd46984af8db5d342d5bff2e0b95ddc5e4a853bedf4905e698ffde31d"
},
"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"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"One dimensional, fixed-length, immutable sequence"
]
},
{
"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,
"text": [
"(1, 2, 3)"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"list_1 = [1, 2, 3]"
],
"language": "python",
"metadata": {},
"outputs": [],
2015-01-25 21:18:02 +08:00
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Convert to a tuple"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"type(tuple(list_1))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-25 21:18:02 +08:00
"prompt_number": 3,
"text": [
"tuple"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 3
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nested tuples"
]
},
{
"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,
"text": [
"([1, 2, 3], (4, 5))"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Access by index O(1)"
]
},
{
"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,
"text": [
"[1, 2, 3]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Although tuples are immutable, their contents can contain mutable objects"
]
},
{
"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,
"text": [
"[1, 2, 3, 4]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Concatenate tuples by creating a new tuple and copying objects"
]
},
{
"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,
"text": [
"(1, 3, 2, 4, 5, 6)"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Multiply copies references to objects (objects themselves are not copied)"
]
},
{
"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,
"text": [
"('foo', 'bar', 'foo', 'bar')"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Unpack tuples"
]
},
{
"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,
"text": [
"([1, 2, 3, 4], (4, 5))"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Unpack nested tuples"
]
},
{
"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,
"text": [
"(1, 2, 3, 4, 4, 5)"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A common use of variable unpacking is when iterating over sequences of tuples or lists"
]
},
{
"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
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2015-01-25 21:18:02 +08:00
"## list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"One dimensional, variable-length, mutable sequence"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"list_1 = [1, 2, 3]\n",
"list_1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
2015-01-26 01:55:43 +08:00
"prompt_number": 12,
"text": [
"[1, 2, 3]"
]
}
],
2015-01-26 01:55:43 +08:00
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Convert to a list"
]
},
{
"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,
"text": [
"list"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nested list"
]
},
{
"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,
"text": [
"[(1, 2, 3), [4, 5]]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Access by index"
]
},
{
"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,
"text": [
"[4, 5]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 15
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Append an element O(1)"
]
},
{
"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,
"text": [
"[(1, 2, 3), [4, 5], 6]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 16
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Insert an element at a specific index. Insert is expensive as it has to shift subsequent elements O(n)."
]
},
{
"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,
"text": [
"['start', (1, 2, 3), [4, 5], 6]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 17
},
{
"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"
]
},
{
"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,
"text": [
"[(1, 2, 3), [4, 5], 6]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 18
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Remove locates the first such value and removes it O(n)"
]
},
{
"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,
"text": [
"[[4, 5], 6]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 19
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Check if a list contains a value O(n)"
]
},
{
"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,
"text": [
"True"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 20
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Concatenate lists by creating a new list and copying objects"
]
},
{
"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,
"text": [
"[1, 3, 2, 4, 5, 6]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 21
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Extend a list by appending elements. Faster than concatenating lists."
]
},
{
"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,
"text": [
2015-01-25 21:18:02 +08:00
"[[4, 5], 6, 7, 8, 9]"
]
}
],
2015-01-25 21:18:02 +08:00
"prompt_number": 22
}
],
"metadata": {}
}
]
}