如何将数组元组转换为字典?

pep*_*epe 2 python arrays dictionary tuples python-3.x

我有以下元组:

t = (array([0, 1, 2, 3], dtype=uint8), array([1568726,  346469,  589708,   91961]))
Run Code Online (Sandbox Code Playgroud)

我需要转换为dict如下:

dict = {0: 1568726, 1: 346469, 2: 589708, 3: 91961}
Run Code Online (Sandbox Code Playgroud)

我正在尝试

d = dict((x, y) for x, y in t)
Run Code Online (Sandbox Code Playgroud)

但它并没有解决我所拥有的元组的嵌套问题.有什么建议?

另一个SO问题似乎是相似的,但不是:它的主要问题是重新转换dict元素,而这个问题集中在如何将元组中的2个数组连接到dict中.

fal*_*tru 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}
Run Code Online (Sandbox Code Playgroud)