2017-05-11 61 views
2

我有以下元组:如何将数组的元组转换为字典?

t = (array([0, 1, 2, 3], dtype=uint8), array([1568726, 346469, 589708, 91961])) 

,我会需要转换为一个字典如下:

dict = {0: 1568726, 1: 346469, 2: 589708, 3: 91961} 

我与

d = dict((x, y) for x, y in t) 

努力,但它不是解决我所拥有的元组的嵌套问题。有什么建议么?

Another SO question似乎是相似的,但不是:它的主要问题是重新转置字典元素,而这个问题则集中在如何将一个元组中的2个数组连接成字典。

+3

字符'D =字典(邮政编码(* T))的' – inspectorG4dget

+2

可能的复制[python tuple to dict](http://stackoverflow.com/questions/3783530/python-tuple-to-dict) – Baalito

回答

5

您可以使用zip(创建键值对),并dict(到对一个字典转换):

>>> from numpy import array, uint8 
>>> t = (array([0, 1, 2, 3], dtype=uint8), 
     array([1568726, 346469, 589708, 91961])) 
>>> dict(zip(*t)) 
{0: 1568726, 1: 346469, 2: 589708, 3: 91961} 
+0

谢谢,这个很好用 – pepe

+0

@pepe,不客气。快乐的Python编程。 – falsetru