Python:如何根据简单的键和值列表构建字典 [英] Python: how to build a dict from plain list of keys and values
本文介绍了Python:如何根据简单的键和值列表构建字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个值列表,如:
["a", 1, "b", 2, "c", 3]
,我想从中建立这样一个命令:
and I would like to build such a dict from it:
{"a": 1, "b": 2, "c": 3}
在Python中自然的方法是什么?
What is the natural way to do it in Python?
推荐答案
>>> x = ["a", 1, "b", 2, "c", 3]
>>> i = iter(x)
>>> dict(zip(i, i))
{'a': 1, 'c': 3, 'b': 2}
这篇关于Python:如何根据简单的键和值列表构建字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文