numpynumpy-slicing

Multiple indices in NumPy


The following works:

np.array([[10,20,30,40]])[:,[1,3,2]]

How can I do this kind of slicing for a one-dimensional array? In other words, how can I fix the following to make the indices refer to the same dimension instead of three different dimensions?

np.array([10,20,30,40])[1,3,2]

Solution

  • If I understood correctly, just wrap the indices in a list:

    np.array([10,20,30,40])[[1,3,2]]
    # array([20, 40, 30])