5.12. Mapping Generate

5.12.1. Recap

Pair:

>>> data = ('commander', 'Melissa Lewis')

List of pairs:

>>> data = [
...     ('commander', 'Melissa Lewis'),
...     ('botanist', 'Mark Watney'),
...     ('pilot', 'Rick Martinez'),
... ]

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

5.12.3. Enumerate

Function enumerate will create a list of pairs:

>>> crew = ['Melissa Lewis', 'Mark Watney', 'Rick Martinez']
>>>
>>> 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:

>>> crew = ['Melissa Lewis', 'Mark Watney', 'Rick Martinez']
>>>
>>> list(enumerate(crew))  
[(0, 'Melissa Lewis'),
 (1, 'Mark Watney'),
 (2, 'Rick Martinez')]

Evaluate enumerate object to dict instantly:

>>> crew = ['Melissa Lewis', 'Mark Watney', 'Rick Martinez']
>>>
>>> dict(enumerate(crew))  
{0: 'Melissa Lewis',
 1: 'Mark Watney',
 2: 'Rick Martinez'}

5.12.4. Zip

Function zip will create a list of pairs:

>>> 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
>>> roles = ['commander', 'botanist', 'pilot']
>>> crew = ['Melissa Lewis', 'Mark Watney', 'Rick Martinez']
>>>
>>> list(zip(roles, crew))  
[('commander', 'Melissa Lewis'),
 ('botanist', 'Mark Watney'),
 ('pilot', 'Rick Martinez')]
>>> roles = ['commander', 'botanist', 'pilot']
>>> crew = ['Melissa Lewis', 'Mark Watney', 'Rick Martinez']
>>>
>>> dict(zip(roles, crew))  
{'commander': 'Melissa Lewis',
 'botanist': 'Mark Watney',
 'pilot': 'Rick Martinez'}
>>> roles = ['commander', 'botanist', 'pilot']
>>> firstnames = ['Melissa', 'Mark', 'Rick']
>>> lastnames = ['Lewis', 'Watney', 'Martinez']
>>>
>>> result = zip(roles, firstnames, lastnames)
>>>
>>>
>>> next(result)
('commander', 'Melissa', 'Lewis')
>>>
>>> role, fname, lname = next(result)
>>> role
'botanist'
>>> fname
'Mark'
>>> lname
'Watney'
>>> 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
>>> roles = ['commander', 'botanist', 'pilot']
>>> crew = [('Melissa', 'Lewis'), ('Mark', 'Watney'), ('Rick', 'Martinez')]
>>>
>>> list(zip(roles, crew))  
[('commander', ('Melissa', 'Lewis')),
 ('botanist', ('Mark', 'Watney')),
 ('pilot', ('Rick', 'Martinez'))]
>>> roles = ['commander', 'botanist', 'pilot']
>>> crew = [('Melissa', 'Lewis'), ('Mark', 'Watney'), ('Rick', 'Martinez')]
>>>
>>> dict(zip(roles, crew))  
{'commander': ('Melissa', 'Lewis'),
 'botanist': ('Mark', 'Watney'),
 'pilot': ('Rick', 'Martinez')}

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

5.12.6. Assignments

Code 5.6. 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ść

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

Code 5.7. 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ść

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

Code 5.8. 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ść

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 '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 = ...