假设你有:
keys = ['name', 'age', 'food'] values = ['Monty', 42, 'spam']
产生以下字典的最简单方法是什么?
a_dict = {'name' : 'Monty', 'age' : 42, 'food' : 'spam'}
像这样:
>>> keys = ['a', 'b', 'c'] >>> values = [1, 2, 3] >>> dictionary = dict(zip(keys, values)) >>> print(dictionary) {'a': 1, 'b': 2, 'c': 3}
Voila 成对的·构造函数和·函数非常有用: