gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:42:15 26 4
gpt4 key购买 nike

我有以下元组:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com