我有以下元组:
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 个数组连接到字典中。
最佳答案
您可以使用 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}
关于python - 如何将数组元组转换为字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43918839/