将两个列表(一个作为键,一个作为值)合并到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"]))