Constructing a ndarray of values from another ndarray containing dictionary keys

…衆ロ難τιáo~ 提交于 2019-12-06 09:48:43

You can use np.vectorize with dict.get:

d = np.vectorize(mydict.get)

res = d(input_pattern)

print(res)

array([[1, 6],
       [2, 5],
       [3, 4]])

the problem is there is no way to know the shape of the array beforehand.

We can try to use flat + list comprehension + reshape

np.array([mydict[v] for v in input_pattern.flat]).reshape(input_pattern.shape)

array([[1, 6],
       [2, 5],
       [3, 4]])

flat makes an 1-dimensional iterator out of the array and we later on use reshape to recover the shape of input_pattern.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!