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)
# {
#   '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))
# [(0, 'Melissa Lewis'),
#  (1, 'Mark Watney'),
#  (2, 'Alex Vogel')]

dict(enumerate(crew))
# {0: 'Melissa Lewis',
#  1: 'Mark Watney',
#  2: 'Alex Vogel'}

4.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', '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))
# [('commander', 'Melissa Lewis'),
#  ('botanist', 'Mark Watney'),
#  ('chemist', 'Alex Vogel')]

dict(zip(roles, crew))
# {'commander': 'Melissa Lewis',
#  'botanist': 'Mark Watney',
#  'chemist': 'Alex Vogel'}
roles = ['commander', 'botanist', 'chemist']
crew = [('Melissa', 'Lewis'), ('Mark', 'Watney'), ('Alex', 'Vogel')]

astronauts = zip(roles, crew)
astronauts
# <zip object at 0x120c487c0>

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))
# [('commander', ('Melissa', 'Lewis')),
#  ('botanist', ('Mark', 'Watney')),
#  ('chemist', ('Alex', 'Vogel'))]

dict(zip(roles, crew))
# {'commander': ('Melissa', 'Lewis'),
#  'botanist': ('Mark', 'Watney'),
#  'chemist': ('Alex', 'Vogel')}

4.2.5. Assignments

4.2.5.1. Mapping Generate Pairs

English
  1. Use data from "Input" section (see below)

  2. Create result: dict

  3. Convert DATA to dict and assign to result

  4. Compare result with "Output" section (see below)

Polish
  1. Użyj danych z sekcji "Input" (patrz poniżej)

  2. Stwórz result: dict

  3. Przekonwertuj DATA do dict i przypisz do result

  4. Porównaj wyniki z sekcją "Output" (patrz poniżej)

Input
DATA = [
    ('Sepal length', 5.8),
    ('Sepal width', 2.7),
    ('Petal length', 5.1),
    ('Petal width', 1.9),
    ('Species', 'virginica')
]
Output
result: dict
# {'Sepal length': 5.8,
#  'Sepal width': 2.7,
#  'Petal length': 5.1,
#  'Petal width': 1.9,
#  'Species': 'virginica'}

4.2.5.2. Mapping Generate Enumerate

English
  1. Use data from "Input" section (see below)

  2. Create result: dict

  3. Using enumerate() convert data to dict and assign to result

  4. Compare result with "Output" section (see below)

Polish
  1. Użyj danych z sekcji "Input" (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ą "Output" (patrz poniżej)

Input
DATA = ['setosa', 'versicolor', 'virginica']
Output
result: dict
# {0: 'setosa',
#  1: 'versicolor',
#  2: 'virginica'}

4.2.5.3. Mapping Generate Zip

English
  1. Use data from "Input" section (see below)

  2. Create result: dict

  3. Using zip() convert data to dict and assign to result

  4. Compare result with "Output" section (see below)

Polish
  1. Użyj danych z sekcji "Input" (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ą "Output" (patrz poniżej)

Input
KEYS =  ['Sepal length', 'Sepal width', 'Petal length', 'Petal width', 'Species']
VALUES = [5.8, 2.7, 5.1, 1.9, 'virginica']
Output
result: dict
# {'Sepal length': 5.8,
#  'Sepal width': 2.7,
#  'Petal length': 5.1,
#  'Petal width': 1.9,
#  'Species': 'virginica'}