【发布时间】:2017-12-26 20:41:29
【问题描述】:
在 python 中有没有办法将一组列表转换为字典,其中列表的名称是键,值是值?你可以用字典理解来做到这一点吗?
one = ['a', 'b', 'c']
two = ['d','e','f']
会变成
dictionary = {"one" : ['a', 'b', 'c'], "two":['d','e','f'] }
【问题讨论】:
-
一组列表?比如
set?..... -
你必须展示这些是如何填充的。 'one' 是变量的名称吗?函数的参数?
-
"one" 不是“列表名称”。列表对象没有这样的概念。 “one”是绑定到列表的 Python 变量的名称,但您同样可以分配给名为“three”的变量,它仍然是同一个列表。
-
dict(zip(["one", "two"], [one, two]))
标签: python dictionary dictionary-comprehension