gpt4 book ai didi

python - 从两个 numpy 数组创建字典

转载 作者:行者123 更新时间:2023-12-03 14:10:40 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





How do I convert two lists into a dictionary?

(18 个回答)


3年前关闭。




我有以下两个 numpy 数组:

a = array([400., 403., 406.]);
b = array([0.2,0.55,0.6]);

现在我想创建一个字典,其中数组 a 作为键,b 作为相应的值:
dic = { 
400: 0.2,
403: 0.55,
406: 0.6
}

我怎么能做到这一点?

最佳答案

您可以使用带有压缩迭代的快速 for 循环。

import numpy as np
a = np.array([400., 403., 406.]);
b = np.array([0.2,0.55,0.6]);
dict = {}
for A, B in zip(a, b):
dict[A] = B

print(dict)
# {400.0: 0.2, 403.0: 0.55, 406.0: 0.6}

关于python - 从两个 numpy 数组创建字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52207358/

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