如何将两个列表转换成字典? [英] How do I convert two lists into a dictionary?

查看:76
本文介绍了如何将两个列表转换成字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,您有以下列表.

Imagine that you have the following list.

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}

Voila :-)成对的 dict 构造函数和 zip 函数非常有用.

Voila :-) The pairwise dict constructor and zip function are awesomely useful.

这篇关于如何将两个列表转换成字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆