简体   繁体   English

在Python中将两个列表转换为字典列表

[英]Converting two lists to a list of dictionaries in Python

I have two lists for example like this: 我有两个这样的清单:

L = [1, 2]
S = ['B', 'C']

How can I get them to be combined into a dictionary like this: 我如何才能将它们组合成这样的字典:

X = {'B': 1, 'C': 2}

The lists will always be the same length, but can have any amount of items. 列表的长度始终相同,但可以包含任意数量的项目。

这是单线的:

dict(zip(S, L))

This way: 这条路:

>>> key_list = ['a', 'b']
>>> value_list = [1, 2]
>>> result = dict(zip(key_list, value_list))
>>> print result
{'a': 1, 'b': 2}
>>> _

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM