将nd数组转换为键,值字典 [英] Convert nd array to key, value dictionary

查看:84
本文介绍了将nd数组转换为键,值字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python中是否存在将nd数组转换为字典的函数,其中key是索引的元组,value是该索引处的矩阵值?

Is there a function in python to convert an nd-array into a dictionary where key is a tuple of index and value is the matrix value at that index?

例如:

A = np.random.random([3,4,5])

结果:

{(i,j,k): A[i,j,k]}

推荐答案

当然:您可以使用np.ndenumerate完成此操作:

Sure: you can use np.ndenumerate to accomplish this:

{ index: v for index, v in np.ndenumerate(A) }

或者像DSM所指出的那样简单:

Or simply, as DSM pointed out:

dict(np.ndenumerate(A))

这篇关于将nd数组转换为键,值字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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