简体   繁体   English

索引数组到元素数组

[英]Numpy array of indexes to array of elements

I have a MxN numpy array containing ints, representing indexes of a big array of size K How do I convert efficiently my M*N array of indexes to an MxN array of elements? 我有一个包含整数的MxN numpy数组,表示大小为K的大数组的索引。如何有效地将索引的M * N数组转换为元素的MxN数组?

Example : 范例:

K = ['a','b','c','d']
M = [[0,3],[2,1]]

Result : 结果:

[['a','d'],['c','b']]

Thank you! 谢谢!

We can make numpy arrays from these lists: 我们可以从以下列表中创建numpy数组:

import numpy as np

k = np.array(K)
m = np.array(M)

and then perform a mapping with k[m] : 然后使用k[m]进行映射:

>>> k[m]
array([['a', 'd'],
       ['c', 'b']], dtype='<U1')

Here for every element in m , we thus "replace" it with the element stored in k at the index of the original value of m at that location. 因此,对于m每个元素,我们将其“替换”为存储在k中的元素,该元素位于该位置m的原始值的索引处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM