As an alternative, suppose you are interested in "array indexing", i.e., getting an array of values back of length L
, where each value in the array is from a location in A
. You would like to indicate these locations via an index of some kind.
Suppose A
is N
-dimensional. Array indexing solves this problem by letting you pass a tuple of N
lists of length L
. The i
th list gives the indices corresponding to the i
th dimension in A
. In the simplest case, the index lists can be length-one (L=1
):
>>> A[[1], [2]]
array([7])
But the index lists could be longer (L=5
):
>>> A[[1,1,1,1,0], [2,2,2,2,0]]
array([7, 7, 7, 7, 1])
I emphasize that this is a tuple of lists; you can also go:
>>> first_axis, second_axis = [1,1,1,1,0], [2,2,2,2,0]
>>> A[(first_axis,second_axis)]
array([7, 7, 7, 7, 1])
You can also pass a non-tuple sequence (a list of lists rather than a tuple of lists) A[[first_axis,second_axis]]
but in NumPy version 1.21.2, a FutureWarning
is given noting that this is deprecated.
Another example here: https://stackoverflow.com/a/23435869/4901005