6.11. Mapping Generate¶
6.11.1. Recap¶
Pair:
>>> data = ('commander', 'Melissa Lewis')
List of pairs:
>>> data = [
... ('commander', 'Melissa Lewis'),
... ('botanist', 'Mark Watney'),
... ('pilot', 'Rick Martinez'),
... ]
6.11.2. List of Pairs¶
>>> data = [
... ('commander', 'Melissa Lewis'),
... ('botanist', 'Mark Watney'),
... ('pilot', 'Rick Martinez')
... ]
>>>
>>> dict(data)
{'commander': 'Melissa Lewis',
'botanist': 'Mark Watney',
'pilot': 'Rick Martinez'}
6.11.3. Enumerate¶
SetUp:
>>> crew = ['Melissa Lewis', 'Mark Watney', 'Rick Martinez']
Function enumerate
will create a list of pairs:
>>> result = enumerate(crew)
>>>
>>> next(result)
(0, 'Melissa Lewis')
>>>
>>> next(result)
(1, 'Mark Watney')
>>>
>>> next(result)
(2, 'Rick Martinez')
>>>
>>> next(result)
Traceback (most recent call last):
StopIteration
Evaluate enumerate object to list instantly:
>>> list(enumerate(crew))
[(0, 'Melissa Lewis'),
(1, 'Mark Watney'),
(2, 'Rick Martinez')]
Evaluate enumerate object to dict instantly:
>>> dict(enumerate(crew))
{0: 'Melissa Lewis',
1: 'Mark Watney',
2: 'Rick Martinez'}
6.11.4. Zip¶
SetUp:
>>> roles = ['commander', 'botanist', 'pilot']
>>> crew = ['Melissa Lewis', 'Mark Watney', 'Rick Martinez']
Function zip
will create a list of pairs:
>>> result = zip(roles, crew)
>>>
>>> next(result)
('commander', 'Melissa Lewis')
>>>
>>> next(result)
('botanist', 'Mark Watney')
>>>
>>> next(result)
('pilot', 'Rick Martinez')
>>>
>>> next(result)
Traceback (most recent call last):
StopIteration
Evaluate zip object to list instantly:
>>> list(zip(roles, crew))
[('commander', 'Melissa Lewis'),
('botanist', 'Mark Watney'),
('pilot', 'Rick Martinez')]
Evaluate zip object to dict instantly:
>>> dict(zip(roles, crew))
{'commander': 'Melissa Lewis',
'botanist': 'Mark Watney',
'pilot': 'Rick Martinez'}
6.11.5. Use Case - 0x01¶
>>> months = ['January', 'February', 'March', 'April']
>>>
>>>
>>> dict(enumerate(months))
{0: 'January', 1: 'February', 2: 'March', 3: 'April'}
>>>
>>> dict(enumerate(months, start=1))
{1: 'January', 2: 'February', 3: 'March', 4: 'April'}
6.11.6. Use Case - 0x02¶
>>> DATA = """10, 4, virginica, setosa, versicolor
... 5.8, 2.7, 5.1, 1.9, 0
... 5.1, 3.5, 1.4, 0.2, 1
... 5.7, 2.8, 4.1, 1.3, 2
... 6.3, 2.9, 5.6, 1.8, 0
... 6.4, 3.2, 4.5, 1.5, 2
... 4.7, 3.2, 1.3, 0.2, 1
... 7.0, 3.2, 4.7, 1.4, 2
... 7.6, 3.0, 6.6, 2.1, 0
... 4.9, 3.0, 1.4, 0.2, 1
... 4.9, 2.5, 4.5, 1.7, 0"""
>>> header, *rows = DATA.splitlines()
>>> nrows, nfeatures, *class_labels = header.split(', ')
>>> label_encoder = dict(enumerate(class_labels))
>>> nrows
'10'
>>>
>>> nfeatures
'4'
>>>
>>> class_labels
['virginica', 'setosa', 'versicolor']
>>>
>>> label_encoder
{0: 'virginica', 1: 'setosa', 2: 'versicolor'}
>>> label_encoder[0]
'virginica'
>>>
>>> label_encoder[1]
'setosa'
>>>
>>> label_encoder[2]
'versicolor'
6.11.7. Use Case - 0x03¶
SetUp:
>>> roles = ['commander', 'botanist', 'pilot']
>>> crew = [('Melissa', 'Lewis'), ('Mark', 'Watney'), ('Rick', 'Martinez')]
>>> result = zip(roles, crew)
>>>
>>> next(result)
('commander', ('Melissa', 'Lewis'))
>>>
>>> next(result)
('botanist', ('Mark', 'Watney'))
>>>
>>> next(result)
('pilot', ('Rick', 'Martinez'))
>>>
>>> next(result)
Traceback (most recent call last):
StopIteration
Evaluate zip object to list instantly:
>>> list(zip(roles, crew))
[('commander', ('Melissa', 'Lewis')),
('botanist', ('Mark', 'Watney')),
('pilot', ('Rick', 'Martinez'))]
Evaluate zip object to dict instantly:
>>> dict(zip(roles, crew))
{'commander': ('Melissa', 'Lewis'),
'botanist': ('Mark', 'Watney'),
'pilot': ('Rick', 'Martinez')}
6.11.8. Assignments¶
"""
* Assignment: Mapping Generate Pairs
* Required: yes
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min
English:
1. Define `result: dict`
2. Convert `DATA` to `dict` and assign to `result`
3. Run doctests - all must succeed
Polish:
1. Zdefiniuj `result: dict`
2. Przekonwertuj `DATA` do `dict` i przypisz do `result`
3. Uruchom doctesty - wszystkie muszą się powieść
Hints:
* `dict()`
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> assert type(result) is dict, \
'Variable `result` has invalid type, should be dict'
>>> assert all(type(x) is str for x in result.keys()), \
'All dict keys should be str'
>>> assert 'Sepal length' in result.keys()
>>> assert 'Sepal width' in result.keys()
>>> assert 'Petal length' in result.keys()
>>> assert 'Petal width' in result.keys()
>>> assert 'Species' in result.keys()
>>> assert 5.8 in result.values()
>>> assert 2.7 in result.values()
>>> assert 5.1 in result.values()
>>> assert 1.9 in result.values()
>>> assert 'virginica' in result.values()
>>> result # doctest: +NORMALIZE_WHITESPACE
{'Sepal length': 5.8,
'Sepal width': 2.7,
'Petal length': 5.1,
'Petal width': 1.9,
'Species': 'virginica'}
"""
DATA = [
('Sepal length', 5.8),
('Sepal width', 2.7),
('Petal length', 5.1),
('Petal width', 1.9),
('Species', 'virginica')]
# Dict with converted DATA
# type: dict[str,float|str]
result = ...
"""
* Assignment: Mapping Generate Enumerate
* Required: yes
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min
English:
1. Define `result: dict`
2. Assign to `result` converted `DATA` to `dict`
3. Use `enumerate()`
4. Run doctests - all must succeed
Polish:
1. Zdefiniu `result: dict`
2. Przypisz do `result` przekonwertowane `DATA` do `dict`
3. Użyj `enumerate()`
4. Uruchom doctesty - wszystkie muszą się powieść
Hints:
* `dict()`
* `enumerate()`
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> assert type(result) is dict, \
'Variable `result` has invalid type, should be dict'
>>> assert all(type(x) is int for x in result.keys()), \
'All dict keys should be int'
>>> assert all(type(x) is str for x in result.values()), \
'All dict values should be str'
>>> result
{0: 'setosa', 1: 'versicolor', 2: 'virginica'}
"""
DATA = ['setosa', 'versicolor', 'virginica']
# Dict with enumerated DATA
# type: dict[int,str]
result = ...
"""
* Assignment: Mapping Generate Zip
* Required: yes
* Complexity: easy
* Lines of code: 1 lines
* Time: 2 min
English:
1. Define `result: dict`
2. Assign to `result` zipped `KEYS` and `VALUES` to `dict`
3. Use `zip()`
4. Run doctests - all must succeed
Polish:
1. Zdefiniuj `result: dict`
2. Przypisz do `result` zzipowane `KEYS` i `VALUES` do `dict`
3. Użyj `zip()`
4. Uruchom doctesty - wszystkie muszą się powieść
Hints:
* `dict()`
* `zip()`
Tests:
>>> import sys; sys.tracebacklimit = 0
>>> assert type(result) is dict, \
'Variable `result` has invalid type, should be dict'
>>> assert all(type(x) is str for x in result.keys()), \
'All dict keys should be str'
>>> assert 'SepalLength' in result.keys()
>>> assert 'SepalWidth' in result.keys()
>>> assert 'PetalLength' in result.keys()
>>> assert 'PetalWidth' in result.keys()
>>> assert 'Species' in result.keys()
>>> assert 5.8 in result.values()
>>> assert 2.7 in result.values()
>>> assert 5.1 in result.values()
>>> assert 1.9 in result.values()
>>> assert 'virginica' in result.values()
>>> result # doctest: +NORMALIZE_WHITESPACE
{'SepalLength': 5.8,
'SepalWidth': 2.7,
'PetalLength': 5.1,
'PetalWidth': 1.9,
'Species': 'virginica'}
"""
KEYS = ['SepalLength', 'SepalWidth', 'PetalLength', 'PetalWidth', 'Species']
VALUES = [5.8, 2.7, 5.1, 1.9, 'virginica']
# Dict with Zipped KEYS and VALUES
# type: dict[str,float|str]
result = ...