将两个列表(一个作为键,一个作为值)合并到Python的dict中 [英] Merge two lists,one as keys, one as values, into a dict in Python

查看:431
本文介绍了将两个列表(一个作为键,一个作为值)合并到Python的dict中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python中是否有任何内置函数将两个列表合并成一个dict?喜欢:

Is there any build-in function in Python that merges two lists into a dict? Like:

combined_dict = {}

keys = ["key1","key2","key3"] 

values = ["val1","val2","val3"]

for k,v in zip(keys,values):
    combined_dict[k] = v

其中:

作为包含密钥的列表。

keys acts as the list that contains the keys.

作为包含值的列表

有一个名为 array_combine ,实现这种效果。

There is a function called array_combine that achieves this effect.

推荐答案

这应该可以工作,虽然我猜这不是一个单一功能:

Seems like this should work, though I guess it's not one single function:

dict(zip(["key1","key2","key3"], ["val1","val2","val3"]))

从这里:如何我将两个列表合并成Pytho的字典n?

这篇关于将两个列表(一个作为键,一个作为值)合并到Python的dict中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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