我有一个元组如下:
(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/