这个问题在这里已经有了答案:
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/