python - 在python中将元组转换为字典

标签 python dictionary tuples

我有一个元组如下:

(1, ['a', 'b', 'c'])

如何将这样的元组转换为以下字典?

{1: ['a', 'b', 'c’]}`

我的实际元组如下所示:

a = (0, ['http://www.armslist.com/posts/2997703/pittsburgh-pennsylvania-handguns-for-sale--80--1911-frame', 'http://www.armslist.com/posts/4240186/racine-wisconsin-rifles-for-sale--stainless-winchester-m70-300wsm'])

当我尝试使用 dict(a) 将其转换为字典时,出现错误:

TypeError: cannot convert dictionary update sequence element #0 to a sequence

如何解决?

最佳答案

您可以将可迭代的元组传递给dict:

In [22]: dict([(1,['a','b','c'])])
Out[22]: {1: ['a', 'b', 'c']}

关于python - 在python中将元组转换为字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33491261/

相关文章:

python - 从以 basemap 位置为中心的 networkx 绘制图形

c++ - std::map::find()

javascript - 以更短的方式从数组中获取字符串

haskell - 将列表拆分为可能的元组列表

python - 如何从字典中删除键和值 float

python - python 中的 Unicode 问题

Python 将具有偶然模式的列表中的元素提取到元组中

scala - 分解函数参数中的元组

在 rc.local 中启动时出现 python 脚本问题(没有名为 ... 的模块)

vba - 尝试从 VBA 字典中检索值,如果 key 未使用,则会引发错误?