How do I write elements of list B
in terms of array indices of A
? The desired output is attached.
import numpy as np
A = np.array([[1,2,3],[4,5,6],[7,8,9]])
B = [1,5,6]
The desired output is
B=[A[0,0], A[1,1], A[1,2]]
CodePudding user response:
You could use numpy.isin
to see if the elements in B
exist in A
; then use numpy.where
to find the indexes of the elements that exist. Since the indexes are separated by axes, you could finally, you could unpack and zip
them to get the desired outcome:
out = list(zip(*np.where(np.isin(A, B))))
For A=np.array([[1,2,3],[4,5,6],[7,8,9]])
and B=[1,5,6,7]
, the output:
[(0, 0), (1, 1), (1, 2), (2, 0)]