Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python将列表转换为dict_Python - Fatal编程技术网

如何使用python将列表转换为dict

如何使用python将列表转换为dict,python,Python,我有一张这样的清单 [(u'name1', (47.5320299939, 7.70498245944), (47.5321349987, 7.70499587048), (47.5319710886, 7.70484834899), (47.5320299939, 7.70498245944)),(u'name2', (47.5320299939, 7.70498245944), (47.5321349987, 7.70499587048), (47.5319710886, 7.704848

我有一张这样的清单

 [(u'name1', (47.5320299939, 7.70498245944), (47.5321349987, 7.70499587048), (47.5319710886, 7.70484834899), (47.5320299939, 7.70498245944)),(u'name2', (47.5320299939, 7.70498245944), (47.5321349987, 7.70499587048), (47.5319710886, 7.70484834899), (47.5320299939, 7.70498245944))]
我想把列表转换成dict,类似这样的东西

{'name1': (47.5320299939, 7.70498245944), (47.5321349987, 7.70499587048), (47.5319710886, 7.70484834899), (47.5320299939, 7.70498245944)}

我想用python来做。任何人都可以帮忙。

字典理解可以做到这一点:

{item[0]: item[1:] for item in inputlist}
因为输入元素是元组,所以输出值也是元组:

>>> inputlist = [(u'name1', (47.5320299939, 7.70498245944), (47.5321349987, 7.70499587048), (47.5319710886, 7.70484834899), (47.5320299939, 7.70498245944)),(u'name2', (47.5320299939, 7.70498245944), (47.5321349987, 7.70499587048), (47.5319710886, 7.70484834899), (47.5320299939, 7.70498245944))]
>>> {item[0]: item[1:] for item in inputlist}
{u'name2': ((47.5320299939, 7.70498245944), (47.5321349987, 7.70499587048), (47.5319710886, 7.70484834899), (47.5320299939, 7.70498245944)), u'name1': ((47.5320299939, 7.70498245944), (47.5321349987, 7.70499587048), (47.5319710886, 7.70484834899), (47.5320299939, 7.70498245944))}
>>> pprint({item[0]: item[1:] for item in inputlist})
{u'name1': ((47.5320299939, 7.70498245944),
            (47.5321349987, 7.70499587048),
            (47.5319710886, 7.70484834899),
            (47.5320299939, 7.70498245944)),
 u'name2': ((47.5320299939, 7.70498245944),
            (47.5321349987, 7.70499587048),
            (47.5319710886, 7.70484834899),
            (47.5320299939, 7.70498245944))}

那不是一本有效的字典;您的意思是将元组集中到一个列表值中吗?输入中的
name2
发生了什么事?可能是重复的@Martinjn Pieters:谢谢Martijn。成功了。这就是我想要的。