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?
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:
import numpy as np
k = np.array(K)
m = np.array(M)
and then perform a mapping with 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.