How To Access Numpy Array With A Set Of Indices Stored In Another Numpy Array?
I have a numpy array which stores a set of indices I need to access another numpy array. I tried to use a for loop but it doesn't work as I expected. The situation is like this: &g
Solution 1:
Convert it to a tuple
:
>>> a[tuple(c[0])]
1
Because list
and array
indices trigger advanced indexing. tuple
s are (mostly) basic slicing.
Solution 2:
Index a
with columns of c
by passing the first column as row's index and second one as column index:
In [23]: a[c[:,0], c[:,1]]
Out[23]: array([1, 2])
Post a Comment for "How To Access Numpy Array With A Set Of Indices Stored In Another Numpy Array?"