mirror of
https://github.com/donnemartin/data-science-ipython-notebooks.git
synced 2024-03-22 13:30:56 +08:00
Updated markdown discussions for clarity and consistency.
This commit is contained in:
parent
03597adc58
commit
3574594172
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"metadata": {
|
||||
"name": "",
|
||||
"signature": "sha256:893799227c6632abab6f36b3299512e1f724d3a9e69a9cc0b81a3ff9c872f370"
|
||||
"signature": "sha256:fed6792b1275b1a5b92e846b93e797942c4eac23ccf1c44b750b469e3f33ffbe"
|
||||
},
|
||||
"nbformat": 3,
|
||||
"nbformat_minor": 0,
|
||||
|
@ -37,6 +37,13 @@
|
|||
"## slice"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Slice selects a section of list types (arrays, tuples, NumPy arrays) with its arguments [start:end]: start is included, end is not. The number of elements in the result is stop - end."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
|
@ -48,7 +55,7 @@
|
|||
"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."
|
||||
"Slice 4 elements starting at index 6 and ending at index 9:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -76,7 +83,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Omit start to default to start of the sequence"
|
||||
"Omit start to default to start of the sequence:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -103,7 +110,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Omit end to default to end of the sequence"
|
||||
"Omit end to default to end of the sequence:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -130,7 +137,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Negative indices slice relative to the end"
|
||||
"Negative indices slice relative to the end:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -157,7 +164,9 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Slice can also take a step such as the one below, which takes every other element"
|
||||
"Slice can also take a step [start:end:step].\n",
|
||||
"\n",
|
||||
"Get every other element:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -211,7 +220,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Assign elements to a slice. Slice range does not have to equal number of elements to assign."
|
||||
"You can assign elements to a slice (note the slice range does not have to equal number of elements to assign):"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -240,7 +249,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Compare assigning into a slice (above) versus assigning into an index (below)"
|
||||
"Compare the output of assigning into a slice (above) versus the output of assigning into an index (below):"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -276,7 +285,9 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Generate a list of evenly spaced integers. Note: range is no longer avalable in Python 3 "
|
||||
"Generate a list of evenly spaced integers with range or xrange. Note: range in Python 3 returns a generator and xrange is not available.\n",
|
||||
"\n",
|
||||
"Generate 10 integers:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -303,7 +314,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Range can take start, stop, and step arguments"
|
||||
"Range can take start, stop, and step arguments:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -330,7 +341,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"It is very common to iterate through sequences by index with range"
|
||||
"It is very common to iterate through sequences by index with range:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -361,7 +372,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"xrange is recommended for longer ranges and is available in Python 3 as range. It returns an iterator that generates integers one by one rather than all at once and storing them in a large list."
|
||||
"For longer ranges, xrange is recommended and is available in Python 3 as range. It returns an iterator that generates integers one by one rather than all at once and storing them in a large list."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -416,7 +427,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Find the location where an element should be inserted to keep the list sorted"
|
||||
"Find the location where an element should be inserted to keep the list sorted:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -444,7 +455,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Inserts an element into a location to keep the list sorted"
|
||||
"Insert an element into a location to keep the list sorted:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -508,7 +519,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Sort by secondary key: str length"
|
||||
"Sort by the secondary key of str length:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -544,7 +555,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Return a new sorted list from the elements of a sequence"
|
||||
"Return a new sorted list from the elements of a sequence O(n log n):"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -591,7 +602,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"It's common to get a sorted list of unique elements by combining sorted and set"
|
||||
"It's common to get a sorted list of unique elements by combining sorted and set:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -626,7 +637,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Iterates over the sequence elements in reverse order"
|
||||
"Iterate over the sequence elements in reverse order:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -660,7 +671,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"enumerate gives you the index of a collection and the value"
|
||||
"Get the index of a collection and the value:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -697,7 +708,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Pairs up the elements of sequences to create a list of tuples"
|
||||
"Pair up the elements of sequences to create a list of tuples:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -726,7 +737,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Takes an arbitrary number of sequences. The number of elements it produces is determined by the shortest sequence."
|
||||
"Zip takes an arbitrary number of sequences. The number of elements it produces is determined by the shortest sequence:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -754,7 +765,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"It is common to use zip for simultaneously iterating over multiple sequences combined with enumerate"
|
||||
"It is common to use zip for simultaneously iterating over multiple sequences combined with enumerate:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -783,7 +794,7 @@
|
|||
"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"
|
||||
"Zip can unzip a zipped sequence, which you can think of as converting a list of rows into a list of columns:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -839,7 +850,7 @@
|
|||
"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",
|
||||
"List comprehensions 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",
|
||||
|
@ -856,7 +867,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Convert to upper case all strings that start with a 'b'"
|
||||
"Convert to upper case all strings that start with a 'b':"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -884,7 +895,7 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"List comprehensions can be nested"
|
||||
"List comprehensions can be nested:"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -919,7 +930,9 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Create a lookup map of strings and their locations in the list for strings that start with a 'b'"
|
||||
"A dict comprehension is similar to a list comprehension but returns a dict.\n",
|
||||
"\n",
|
||||
"Create a mapping of strings and their locations in the list for strings that start with a 'b':"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -953,7 +966,9 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Get the unique lengths of strings that start with a 'b'"
|
||||
"A set comprehension is similar to a list comprehension but returns a set.\n",
|
||||
"\n",
|
||||
"Get the unique lengths of strings that start with a 'b':"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue
Block a user