1

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!

2
  • 4
    ........ K[M]? Commented Nov 5, 2018 at 14:14
  • Welp, now I feel dumb. Thank you! Commented Nov 5, 2018 at 14:15

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.