我有一个元组如下:
(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:无法将字典更新序列元素#0转换为序列
我该如何解决?
答案 0 :(得分:2)
您可以将一个可迭代的元组传递给dict
:
In [22]: dict([(1,['a','b','c'])])
Out[22]: {1: ['a', 'b', 'c']}
答案 1 :(得分:0)
使用dict([a])
In[56]: 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'])
In[57]: dict([a])
Out[57]:
{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']}