将元组转换为字典在Python中

3 投票
1 回答
1014 浏览
提问于 2025-04-16 14:23

可能重复的问题:
将元组转换为字典

我该如何将一个包含元组的列表,比如:

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.

撰写回答