6.2. Mapping Generate

6.2.1. Pair

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

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

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

6.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')}

6.2.5. Assignments

Code 6.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()
    ...     and 'Sepal width' in result.keys()
    ...     and 'Petal length' in result.keys()
    ...     and 'Petal width' in result.keys()
    ...     and 'Species' in result.keys())

    >>> assert (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())

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

Code 6.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  # doctest: +NORMALIZE_WHITESPACE
    {0: 'setosa',
     1: 'versicolor',
     2: 'virginica'}
"""

DATA = ['setosa', 'versicolor', 'virginica']

# dict[int,str]: converted enumeration of DATA
result = ...

Code 6.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` converted `DATA` to `dict`
    3. Use `zip()`
    4. Run doctests - all must succeed

Polish:
    1. Zdefiniuj `result: dict`
    2. Przypisz do `result` przekonwertowane `DATA` 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()
    ...     and 'Sepal width' in result.keys()
    ...     and 'Petal length' in result.keys()
    ...     and 'Petal width' in result.keys()
    ...     and 'Species' in result.keys())

    >>> assert (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())

    >>> 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]: converted zipped KEYS and VALUES
result = ...