Files
coding-mentor-agent/kb/python-course-kb-practical-python/wiki/exercises/2-25-making-dictionaries.md
T

1.1 KiB
Raw Blame History

id, source_exercise_id, title, section, source_path, source_repo, source_commit, student_visible_solution, has_private_solution, skip
id source_exercise_id title section source_path source_repo source_commit student_visible_solution has_private_solution skip
practical-python-2.25 2.25 Making dictionaries 2.7 Objects 02_Working_with_data/07_Objects.md https://github.com/dabeaz-course/practical-python 93dca856b41c61a0a0f85ae334116e4c125629ea false false false

Exercise 2.25: Making dictionaries

Source: Practical Python Programming, 02_Working_with_data/07_Objects.md.

Exercise 2.25: Making dictionaries

Remember how the dict() function can easily make a dictionary if you have a sequence of key names and values? Let’s make a dictionary from the column headers:

>>> headers
['name', 'shares', 'price']
>>> converted
['AA', 100, 32.2]
>>> dict(zip(headers, converted))
{'price': 32.2, 'name': 'AA', 'shares': 100}
>>>

Of course, if you’re up on your list-comprehension fu, you can do the whole conversion in a single step using a dict-comprehension:

>>> { name: func(val) for name, func, val in zip(headers, types, row) }
{'price': 32.2, 'name': 'AA', 'shares': 100}
>>>

关联来源