从两个Numpy数组创建字典 [英] Create dictionary from two numpy arrays

查看:128
本文介绍了从两个Numpy数组创建字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个numpy数组:

I have the following two numpy arrays:

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

现在,我想创建一个字典,其中数组a作为键,b作为对应值:

Now I would like to create a dictionary where the array a acts as keys and b as corresponding values:

dic = { 
  400: 0.2,
  403: 0.55,
  406: 0.6
}

我该如何实现?

推荐答案

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

You can use a quick for loop with zipped iterables.

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}

这篇关于从两个Numpy数组创建字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆