mirror of
https://github.com/donnemartin/data-science-ipython-notebooks.git
synced 2024-03-22 13:30:56 +08:00
Converted python comments to markdown to improve readability. Tweaked slice snippets.
This commit is contained in:
parent
da26235b6d
commit
661b71035b
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"metadata": {
|
||||
"name": "",
|
||||
"signature": "sha256:f24f6112f8e8e28d262a4f84be84d1ae459154a9a445687d55392ff177e6a03c"
|
||||
"signature": "sha256:80ff2bfd9335618eb25e15d2708a5703a0249ba8c3beb2c78875bd7a0b8c8e3c"
|
||||
},
|
||||
"nbformat": 3,
|
||||
"nbformat_minor": 0,
|
||||
|
@ -22,11 +22,17 @@
|
|||
"## tuple"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"One dimensional, fixed-length, immutable sequence"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# One dimensional, fixed-length, immutable sequence\n",
|
||||
"tup = (1, 2, 3)\n",
|
||||
"tup"
|
||||
],
|
||||
|
@ -55,11 +61,17 @@
|
|||
"outputs": [],
|
||||
"prompt_number": 2
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Convert to a tuple"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Convert to a tuple\n",
|
||||
"type(tuple(a_list))"
|
||||
],
|
||||
"language": "python",
|
||||
|
@ -76,11 +88,17 @@
|
|||
],
|
||||
"prompt_number": 3
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Nested tuples"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Nested tuples\n",
|
||||
"nested_tup = ([1, 2, 3], (4, 5))\n",
|
||||
"nested_tup"
|
||||
],
|
||||
|
@ -98,11 +116,17 @@
|
|||
],
|
||||
"prompt_number": 4
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Access by index O(1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Access by index O(1)\n",
|
||||
"nested_tup[0]"
|
||||
],
|
||||
"language": "python",
|
||||
|
@ -119,11 +143,17 @@
|
|||
],
|
||||
"prompt_number": 5
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Although tuples are immutable, their contents can contain mutable objects"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Although tuples are immutable, their contents can contain mutable objects\n",
|
||||
"nested_tup[0].append(4)\n",
|
||||
"nested_tup[0]"
|
||||
],
|
||||
|
@ -141,12 +171,17 @@
|
|||
],
|
||||
"prompt_number": 6
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Concatenate tuples by creating a new tuple and copying objects"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Concatenate tuples\n",
|
||||
"# Creates a new tuple and copies objects\n",
|
||||
"(1, 3, 2) + (4, 5, 6)"
|
||||
],
|
||||
"language": "python",
|
||||
|
@ -163,11 +198,17 @@
|
|||
],
|
||||
"prompt_number": 7
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Multiply copies references to objects (objects themselves are not copied)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Multiply copies references to objects (objects themselves are not copied)\n",
|
||||
"('foo', 'bar') * 2"
|
||||
],
|
||||
"language": "python",
|
||||
|
@ -184,11 +225,17 @@
|
|||
],
|
||||
"prompt_number": 8
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Unpack tuples"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Unpack tuples\n",
|
||||
"a, b = nested_tup\n",
|
||||
"a, b"
|
||||
],
|
||||
|
@ -206,11 +253,17 @@
|
|||
],
|
||||
"prompt_number": 9
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Unpack nested tuples"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Unpack nested tuples\n",
|
||||
"(a, b, c, d), (e, f) = nested_tup\n",
|
||||
"a, b, c, d, e, f"
|
||||
],
|
||||
|
@ -228,12 +281,17 @@
|
|||
],
|
||||
"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": [
|
||||
"# A common use of variable unpacking is when iterating over sequences\n",
|
||||
"# of tuples or lists\n",
|
||||
"seq = [( 1, 2, 3), (4, 5, 6), (7, 8, 9)] \n",
|
||||
"for a, b, c in seq: \n",
|
||||
" print(a, b, c)"
|
||||
|
@ -260,11 +318,17 @@
|
|||
"## list"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"One dimensional, variable-length, mutable sequence"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# One dimensional, variable-length, mutable sequence\n",
|
||||
"a_list = [1, 2, 3]\n",
|
||||
"a_list"
|
||||
],
|
||||
|
@ -282,11 +346,17 @@
|
|||
],
|
||||
"prompt_number": 12
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Convert to a list"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Convert to a list\n",
|
||||
"type(list(tup))"
|
||||
],
|
||||
"language": "python",
|
||||
|
@ -303,11 +373,17 @@
|
|||
],
|
||||
"prompt_number": 13
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Nested list"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Nested list\n",
|
||||
"nested_list = [(1, 2, 3), [4, 5]]\n",
|
||||
"nested_list"
|
||||
],
|
||||
|
@ -325,11 +401,17 @@
|
|||
],
|
||||
"prompt_number": 14
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Access by index"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Access by index\n",
|
||||
"nested_list[1]"
|
||||
],
|
||||
"language": "python",
|
||||
|
@ -346,11 +428,17 @@
|
|||
],
|
||||
"prompt_number": 15
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Append an element O(1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Append an element O(1)\n",
|
||||
"nested_list.append(6)\n",
|
||||
"nested_list"
|
||||
],
|
||||
|
@ -368,12 +456,17 @@
|
|||
],
|
||||
"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": [
|
||||
"# Insert an element at a specific index\n",
|
||||
"# Insert is expensive as it has to shift subsequent elements O(n)\n",
|
||||
"nested_list.insert(0, 'start')\n",
|
||||
"nested_list"
|
||||
],
|
||||
|
@ -391,13 +484,17 @@
|
|||
],
|
||||
"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": [
|
||||
"# Pop removes and returns an element from a specified index\n",
|
||||
"# Pop is expensive as it has to shift subsequent elements O(n)\n",
|
||||
"# O(1) if pop is used for the last element\n",
|
||||
"nested_list.pop(0)\n",
|
||||
"nested_list"
|
||||
],
|
||||
|
@ -415,11 +512,17 @@
|
|||
],
|
||||
"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": [
|
||||
"# Remove locates the first such value and removes it O(n)\n",
|
||||
"nested_list.remove((1, 2, 3))\n",
|
||||
"nested_list"
|
||||
],
|
||||
|
@ -437,11 +540,17 @@
|
|||
],
|
||||
"prompt_number": 19
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Check if a list contains a value O(n)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Check if a list contains a value O(n)\n",
|
||||
"6 in nested_list"
|
||||
],
|
||||
"language": "python",
|
||||
|
@ -458,12 +567,17 @@
|
|||
],
|
||||
"prompt_number": 20
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Concatenate lists by creating a new list and copying objects"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Concatenate lists\n",
|
||||
"# Creates a new list and copies objects\n",
|
||||
"[1, 3, 2] + [4, 5, 6]"
|
||||
],
|
||||
"language": "python",
|
||||
|
@ -480,12 +594,17 @@
|
|||
],
|
||||
"prompt_number": 21
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Extend a list by appending elements. Faster than concatenating lists."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Extend a list by appending elements\n",
|
||||
"# Faster than concatenating lists\n",
|
||||
"nested_list.extend([7, 8, 9])\n",
|
||||
"nested_list"
|
||||
],
|
||||
|
@ -510,11 +629,17 @@
|
|||
"## sort"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Sort in-place O(n log n)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Sort in-place O(n log n)\n",
|
||||
"a_list = [1, 5, 3, 9, 7, 6]\n",
|
||||
"a_list.sort()\n",
|
||||
"a_list"
|
||||
|
@ -533,11 +658,17 @@
|
|||
],
|
||||
"prompt_number": 23
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Sort by secondary key: str length"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Sort by secondary key: str length\n",
|
||||
"b_list = ['the', 'quick', 'brown', 'fox', 'jumps', 'over']\n",
|
||||
"b_list.sort(key=len)\n",
|
||||
"b_list"
|
||||
|
@ -563,17 +694,35 @@
|
|||
"## bisect"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# The bisect module does not check whether the list is sorted, as this check\n",
|
||||
"# would be expensive O(n). Using bisect on an unsorted list will not result\n",
|
||||
"# in an error but could lead to incorrect results.\n",
|
||||
"import bisect\n",
|
||||
"\n",
|
||||
"# Find the location where an element should be inserted to keep the\n",
|
||||
"# list sorted\n",
|
||||
"c_list = [1, 2, 2, 3, 5, 13]\n",
|
||||
"bisect.bisect(c_list, 8)"
|
||||
],
|
||||
|
@ -583,19 +732,25 @@
|
|||
{
|
||||
"metadata": {},
|
||||
"output_type": "pyout",
|
||||
"prompt_number": 25,
|
||||
"prompt_number": 26,
|
||||
"text": [
|
||||
"5"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 25
|
||||
"prompt_number": 26
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Inserts an element into a location to keep the list sorted"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Inserts an element into a location to keep the list sorted\n",
|
||||
"bisect.insort(c_list, 8)\n",
|
||||
"c_list"
|
||||
],
|
||||
|
@ -605,13 +760,13 @@
|
|||
{
|
||||
"metadata": {},
|
||||
"output_type": "pyout",
|
||||
"prompt_number": 26,
|
||||
"prompt_number": 27,
|
||||
"text": [
|
||||
"[1, 2, 2, 3, 5, 8, 13]"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 26
|
||||
"prompt_number": 27
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
|
@ -627,58 +782,46 @@
|
|||
"![alt text](http://www.nltk.org/images/string-slicing.png)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"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."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Select a section of list types (arrays, tuples, NumPy arrays)\n",
|
||||
"# start:stop\n",
|
||||
"# start is included, stop is not\n",
|
||||
"# number of elements in the result is stop - start\n",
|
||||
"d_list = 'Monty Python'\n",
|
||||
"d_list[6:10]"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"metadata": {},
|
||||
"output_type": "pyout",
|
||||
"prompt_number": 27,
|
||||
"text": [
|
||||
"'Pyth'"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 27
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Omit start to default to start of the sequence\n",
|
||||
"d_list[:5]"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"metadata": {},
|
||||
"output_type": "pyout",
|
||||
"prompt_number": 28,
|
||||
"text": [
|
||||
"'Monty'"
|
||||
"'Pyth'"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 28
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Omit start to default to start of the sequence"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Omit end to default to end of the sequence\n",
|
||||
"d_list[6:]"
|
||||
"d_list[:5]"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
|
@ -688,18 +831,24 @@
|
|||
"output_type": "pyout",
|
||||
"prompt_number": 29,
|
||||
"text": [
|
||||
"'Python'"
|
||||
"'Monty'"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 29
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Omit end to default to end of the sequence"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Negative indices slice relative to the end\n",
|
||||
"d_list[-12:-7]"
|
||||
"d_list[6:]"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
|
@ -709,19 +858,78 @@
|
|||
"output_type": "pyout",
|
||||
"prompt_number": 30,
|
||||
"text": [
|
||||
"'Monty'"
|
||||
"'Python'"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 30
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Negative indices slice relative to the end"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Slice can also take a step such as the one below, which takes\n",
|
||||
"# every other element\n",
|
||||
"e_list[::2]"
|
||||
"d_list[-12:-7]"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"metadata": {},
|
||||
"output_type": "pyout",
|
||||
"prompt_number": 31,
|
||||
"text": [
|
||||
"'Monty'"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 31
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Slice can also take a step such as the one below, which takes every other element"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"d_list[::2]"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"metadata": {},
|
||||
"output_type": "pyout",
|
||||
"prompt_number": 32,
|
||||
"text": [
|
||||
"'MnyPto'"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 32
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Passing -1 for the step reverses the list or tuple:"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"d_list[::-1]"
|
||||
],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
|
@ -731,39 +939,23 @@
|
|||
"output_type": "pyout",
|
||||
"prompt_number": 33,
|
||||
"text": [
|
||||
"[1, 2, 5, 13]"
|
||||
"'nohtyP ytnoM'"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 33
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Passing -1 for the step reverses the list or tuple:\n",
|
||||
"e_list[::-1]"
|
||||
],
|
||||
"language": "python",
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"metadata": {},
|
||||
"output_type": "pyout",
|
||||
"prompt_number": 34,
|
||||
"text": [
|
||||
"[13, ['H', 'a', 'l', 'l'], 5, 3, 2, 1, 1]"
|
||||
"source": [
|
||||
"Assign elements to a slice. Slice range does not have to equal number of elements to assign."
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 34
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Assign elements to a slice\n",
|
||||
"# Slice range does not have to equal number of elements to assign\n",
|
||||
"e_list = [1, 1, 2, 3, 5, 8, 13]\n",
|
||||
"e_list[5:] = ['H', 'a', 'l', 'l']\n",
|
||||
"e_list"
|
||||
|
@ -774,20 +966,27 @@
|
|||
{
|
||||
"metadata": {},
|
||||
"output_type": "pyout",
|
||||
"prompt_number": 31,
|
||||
"prompt_number": 34,
|
||||
"text": [
|
||||
"[1, 1, 2, 3, 5, 'H', 'a', 'l', 'l']"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 31
|
||||
"prompt_number": 34
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Compare assigning into a slice (above) versus assigning into an index (below)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [
|
||||
"# Compare assigning into a slice (above) versus assigning into\n",
|
||||
"# an inde\n",
|
||||
"e_list = [1, 1, 2, 3, 5, 8, 13]\n",
|
||||
"e_list[5] = ['H', 'a', 'l', 'l']\n",
|
||||
"e_list"
|
||||
],
|
||||
"language": "python",
|
||||
|
@ -796,21 +995,13 @@
|
|||
{
|
||||
"metadata": {},
|
||||
"output_type": "pyout",
|
||||
"prompt_number": 32,
|
||||
"prompt_number": 35,
|
||||
"text": [
|
||||
"[1, 1, 2, 3, 5, ['H', 'a', 'l', 'l'], 13]"
|
||||
]
|
||||
}
|
||||
],
|
||||
"prompt_number": 32
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"collapsed": false,
|
||||
"input": [],
|
||||
"language": "python",
|
||||
"metadata": {},
|
||||
"outputs": []
|
||||
"prompt_number": 35
|
||||
}
|
||||
],
|
||||
"metadata": {}
|
||||
|
|
Loading…
Reference in New Issue
Block a user