如何将两个列表转换为字典? [英] How do I convert two lists into a dictionary?
本文介绍了如何将两个列表转换为字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设您有以下列表.
keys = ['name', 'age', 'food']
values = ['Monty', 42, 'spam']
生成以下字典最简单的方法是什么?
What is the simplest way to produce the following dictionary?
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}
瞧:-) 成对的dict
构造函数和 zip
函数非常有用.
Voila :-) The pairwise dict
constructor and zip
function are awesomely useful.
这篇关于如何将两个列表转换为字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文