4.2. Mapping Generate¶
4.2.1. Pair¶
>>> pair = [
... ('commander', 'Melissa Lewis')]
>>>
>>> dict(pair)
{'commander': 'Melissa Lewis'}
4.2.2. List of Pairs¶
>>> pairs = [
... ('commander', 'Melissa Lewis'),
... ('botanist', 'Mark Watney'),
... ('chemist', 'Alex Vogel')]
>>>
>>> dict(pairs) # doctest: +NORMALIZE_WHITESPACE
{'commander': 'Melissa Lewis',
'botanist': 'Mark Watney',
'chemist': 'Alex Vogel'}
4.2.3. Enumerate¶
>>> crew = ['Melissa Lewis', 'Mark Watney', 'Alex Vogel']
>>> astronaut = enumerate(crew)
>>>
>>> next(astronaut)
(0, 'Melissa Lewis')
>>> next(astronaut)
(1, 'Mark Watney')
>>> next(astronaut)
(2, 'Alex Vogel')
>>> next(astronaut)
Traceback (most recent call last):
StopIteration
>>> crew = ['Melissa Lewis', 'Mark Watney', 'Alex Vogel']
>>>
>>> list(enumerate(crew)) # doctest: +NORMALIZE_WHITESPACE
[(0, 'Melissa Lewis'),
(1, 'Mark Watney'),
(2, 'Alex Vogel')]
>>> dict(enumerate(crew)) # doctest: +NORMALIZE_WHITESPACE
{0: 'Melissa Lewis',
1: 'Mark Watney',
2: 'Alex Vogel'}
4.2.4. Zip¶
zip
is a generatorzip
will create a list of pairs (likedict.items()
)
>>> roles = ['commander', 'botanist', 'chemist']
>>> crew = ['Melissa Lewis', 'Mark Watney', 'Alex Vogel']
>>>
>>> astronaut = zip(roles, crew)
>>> next(astronaut)
('commander', 'Melissa Lewis')
>>> next(astronaut)
('botanist', 'Mark Watney')
>>> next(astronaut)
('chemist', 'Alex Vogel')
>>> next(astronaut)
Traceback (most recent call last):
StopIteration
>>> roles = ['commander', 'botanist', 'chemist']
>>> crew = ['Melissa Lewis', 'Mark Watney', 'Alex Vogel']
>>>
>>> list(zip(roles, crew)) # doctest: +NORMALIZE_WHITESPACE
[('commander', 'Melissa Lewis'),
('botanist', 'Mark Watney'),
('chemist', 'Alex Vogel')]
>>>
>>> dict(zip(roles, crew)) # doctest: +NORMALIZE_WHITESPACE
{'commander': 'Melissa Lewis',
'botanist': 'Mark Watney',
'chemist': 'Alex Vogel'}
>>> roles = ['commander', 'botanist', 'chemist']
>>> crew = [('Melissa', 'Lewis'), ('Mark', 'Watney'), ('Alex', 'Vogel')]
>>>
>>> astronauts = zip(roles, crew)
>>> next(astronauts)
('commander', ('Melissa', 'Lewis'))
>>> next(astronauts)
('botanist', ('Mark', 'Watney'))
>>> next(astronauts)
('chemist', ('Alex', 'Vogel'))
>>> next(astronauts)
Traceback (most recent call last):
StopIteration
>>> roles = ['commander', 'botanist', 'chemist']
>>> crew = [('Melissa', 'Lewis'), ('Mark', 'Watney'), ('Alex', 'Vogel')]
>>>
>>> list(zip(roles, crew)) # doctest: +NORMALIZE_WHITESPACE
[('commander', ('Melissa', 'Lewis')),
('botanist', ('Mark', 'Watney')),
('chemist', ('Alex', 'Vogel'))]
>>> dict(zip(roles, crew)) # doctest: +NORMALIZE_WHITESPACE
{'commander': ('Melissa', 'Lewis'),
'botanist': ('Mark', 'Watney'),
'chemist': ('Alex', 'Vogel')}
4.2.5. Assignments¶
"""
* Assignment: Mapping Generate Pairs
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 min
English:
1. Use data from "Given" section (see below)
2. Create `result: dict`
3. Convert `DATA` to `dict` and assign to `result`
4. Compare result with "Tests" section (see below)
Polish:
1. Użyj danych z sekcji "Given" (patrz poniżej)
2. Stwórz `result: dict`
3. Przekonwertuj `DATA` do `dict` i przypisz do `result`
4. Porównaj wyniki z sekcją "Tests" (patrz poniżej)
Tests:
>>> type(result)
<class 'dict'>
>>> all(type(x) is str for x in result.keys())
True
>>> ('Sepal length' in result.keys()
... and 'Sepal width' in result.keys()
... and 'Petal length' in result.keys()
... and 'Petal width' in result.keys()
... and 'Species' in result.keys())
True
>>> (5.8 in result.values()
... and 2.7 in result.values()
... and 5.1 in result.values()
... and 1.9 in result.values()
... and 'virginica' in result.values())
True
>>> result # doctest: +NORMALIZE_WHITESPACE
{'Sepal length': 5.8,
'Sepal width': 2.7,
'Petal length': 5.1,
'Petal width': 1.9,
'Species': 'virginica'}
"""
# Given
DATA = [
('Sepal length', 5.8),
('Sepal width', 2.7),
('Petal length', 5.1),
('Petal width', 1.9),
('Species', 'virginica'),
]
result = ... # dict from DATA
"""
* Assignment: Mapping Generate Enumerate
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 min
English:
1. Use data from "Given" section (see below)
2. Create `result: dict`
3. Using `enumerate()` convert data to `dict` and assign to `result`
4. Compare result with "Tests" section (see below)
Polish:
1. Użyj danych z sekcji "Given" (patrz poniżej)
2. Stwórz `result: dict`
3. Używając `enumerate()` przekonwertuj dane do `dict` i przypisz do `result`
4. Porównaj wyniki z sekcją "Tests" (patrz poniżej)
Tests:
>>> type(result)
<class 'dict'>
>>> all(type(x) is int for x in result.keys())
True
>>> all(type(x) is str for x in result.values())
True
>>> result # doctest: +NORMALIZE_WHITESPACE
{0: 'setosa',
1: 'versicolor',
2: 'virginica'}
"""
# Given
DATA = ['setosa', 'versicolor', 'virginica']
result = ... # dict from DATA enumeration
"""
* Assignment: Mapping Generate Zip
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 min
English:
1. Use data from "Given" section (see below)
2. Create `result: dict`
3. Using `zip()` convert data to `dict` and assign to `result`
4. Compare result with "Tests" section (see below)
Polish:
1. Użyj danych z sekcji "Given" (patrz poniżej)
2. Stwórz `result: dict`
3. Używając `zip()` przekonwertuj dane do `dict` i przypisz do `result`
4. Porównaj wyniki z sekcją "Tests" (patrz poniżej)
Tests:
>>> type(result)
<class 'dict'>
>>> all(type(x) is str for x in result.keys())
True
>>> ('Sepal length' in result.keys()
... and 'Sepal width' in result.keys()
... and 'Petal length' in result.keys()
... and 'Petal width' in result.keys()
... and 'Species' in result.keys())
True
>>> (5.8 in result.values()
... and 2.7 in result.values()
... and 5.1 in result.values()
... and 1.9 in result.values()
... and 'virginica' in result.values())
True
>>> result # doctest: +NORMALIZE_WHITESPACE
{'Sepal length': 5.8,
'Sepal width': 2.7,
'Petal length': 5.1,
'Petal width': 1.9,
'Species': 'virginica'}
"""
# Given
KEYS = ['Sepal length', 'Sepal width', 'Petal length', 'Petal width', 'Species']
VALUES = [5.8, 2.7, 5.1, 1.9, 'virginica']
result = ... # dict from zipped KEYS and VALUES