9.2. Mapping Generate

9.2.1. Pair

>>> pair = [
...    ('commander', 'Melissa Lewis')]
>>>
>>> dict(pair)
{'commander': 'Melissa Lewis'}

9.2.2. List of Pairs

>>> pairs = [
...    ('commander', 'Melissa Lewis'),
...    ('botanist', 'Mark Watney'),
...    ('chemist', 'Rick Martinez')]
>>>
>>> dict(pairs)  
{'commander': 'Melissa Lewis',
  'botanist': 'Mark Watney',
  'chemist': 'Rick Martinez'}

9.2.3. Enumerate

>>> crew = ['Melissa Lewis', 'Mark Watney', 'Rick Martinez']
>>> astronaut = enumerate(crew)
>>>
>>> next(astronaut)
(0, 'Melissa Lewis')
>>> next(astronaut)
(1, 'Mark Watney')
>>> next(astronaut)
(2, 'Rick Martinez')
>>> next(astronaut)
Traceback (most recent call last):
StopIteration
>>> crew = ['Melissa Lewis', 'Mark Watney', 'Rick Martinez']
>>>
>>> list(enumerate(crew))  
[(0, 'Melissa Lewis'),
 (1, 'Mark Watney'),
 (2, 'Rick Martinez')]
>>> dict(enumerate(crew))  
{0: 'Melissa Lewis',
 1: 'Mark Watney',
 2: 'Rick Martinez'}

9.2.4. Zip

  • zip is a generator

  • zip will create a list of pairs (like dict.items())

>>> roles = ['commander', 'botanist', 'chemist']
>>> crew = ['Melissa Lewis', 'Mark Watney', 'Rick Martinez']
>>>
>>> astronaut = zip(roles, crew)
>>> next(astronaut)
('commander', 'Melissa Lewis')
>>> next(astronaut)
('botanist', 'Mark Watney')
>>> next(astronaut)
('chemist', 'Rick Martinez')
>>> next(astronaut)
Traceback (most recent call last):
StopIteration
>>> roles = ['commander', 'botanist', 'chemist']
>>> crew = ['Melissa Lewis', 'Mark Watney', 'Rick Martinez']
>>>
>>> list(zip(roles, crew))  
[('commander', 'Melissa Lewis'),
 ('botanist', 'Mark Watney'),
 ('chemist', 'Rick Martinez')]
>>>
>>> dict(zip(roles, crew))  
{'commander': 'Melissa Lewis',
 'botanist': 'Mark Watney',
 'chemist': 'Rick Martinez'}
>>> roles = ['commander', 'botanist', 'chemist']
>>> firstnames = ['Melissa', 'Mark', 'Rick']
>>> lastnames = ['Lewis', 'Watney', 'Martinez']
>>>
>>> data = zip(roles, firstnames, lastnames)
>>> next(data)
('commander', 'Melissa', 'Lewis')
>>>
>>> role, fname, lname = next(data)
>>> role
'botanist'
>>> fname
'Mark'
>>> lname
'Watney'
>>> roles = ['commander', 'botanist', 'chemist']
>>> crew = [('Melissa', 'Lewis'), ('Mark', 'Watney'), ('Rick', 'Martinez')]
>>>
>>> astronauts = zip(roles, crew)
>>> next(astronauts)
('commander', ('Melissa', 'Lewis'))
>>> next(astronauts)
('botanist', ('Mark', 'Watney'))
>>> next(astronauts)
('chemist', ('Rick', 'Martinez'))
>>> next(astronauts)
Traceback (most recent call last):
StopIteration
>>> roles = ['commander', 'botanist', 'chemist']
>>> crew = [('Melissa', 'Lewis'), ('Mark', 'Watney'), ('Rick', 'Martinez')]
>>>
>>> list(zip(roles, crew))  
[('commander', ('Melissa', 'Lewis')),
 ('botanist', ('Mark', 'Watney')),
 ('chemist', ('Rick', 'Martinez'))]
>>> dict(zip(roles, crew))  
{'commander': ('Melissa', 'Lewis'),
 'botanist': ('Mark', 'Watney'),
 'chemist': ('Rick', 'Martinez')}

9.2.5. Use Case

>>> 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'}

9.2.6. Assignments

Code 9.5. Solution
"""
* Assignment: Mapping Generate Pairs
* Required: yes
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 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ść

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[str,float|str]: converted DATA to dict
result = ...

Code 9.6. Solution
"""
* Assignment: Mapping Generate Enumerate
* Required: yes
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 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ść

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[int,str]: enumerated DATA to dict
result = ...

Code 9.7. Solution
"""
* Assignment: Mapping Generate Zip
* Required: yes
* Complexity: easy
* Lines of code: 1 lines
* Time: 3 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ść

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'}
"""

KEYS = ['Sepal length', 'Sepal width', 'Petal length',
        'Petal width', 'Species']
VALUES = [5.8, 2.7, 5.1, 1.9, 'virginica']

# dict[str,float|str]: zipped KEYS and VALUES to dict
result = ...