将元组转换为字典在Python中
可能重复的问题:
将元组转换为字典
我该如何将一个包含元组的列表,比如:
seq = [(1, 2), (3, 4), (5, 6)]
转换成一个字典:
dicta = { 1:2, 2:4, 5:6 }
1 个回答
10
>>> seq = [(1, 2), (3, 4), (5, 6)]
>>> dict(seq)
{1: 2, 3: 4, 5: 6}
Python真不错呢 :)
在Python文档中有这样的定义:
The dict() constructor builds dictionaries directly from lists of key-value pairs stored as tuples.