I have high dimension numpy array, the dimension of the array is not fixed. I need to retrieve the value with a index list, the length of the index list is the same as the dimension of the numpy array.
In other words, I need a function:
def get_value_by_list_index(target_array, index_list):
# len(index_list) = target_array.ndim
# target array can be in any dimension
# return the element at index specified on index list
For example, for a 3-dimension array, data and a list [i1, i2, i3], I the function should return data[i1][i2][i3].
Is there a good way to achieve this task?
target_array[tuple(index_list)]which is the same astarget_array[i1,i2,i3]idx=[1,2]be our index list, thenarr[idx]gives the second and third row of a matrix, but ifidx=(1,2), the same statement yields the second row, third column element(), which are just used to reduce ambiguities. @MarcusMüller