Access n-th dimension in python

Viewed 538

I want a easy to read access to some parts of a multidimensional numpy array. For any array accessing the first dimension is easy (b[index]). Accessing the sixth dimension on the other hand is "hard" (especially to read).

b[:,:,:,:,:,index] #the next person to read the code will have to count the :

Is there a better way to do this? Especially is there a way, where the axis is not known while writing the program?

Edit: The indexed dimension is not necessarily the last dimension

5 Answers

In the spirit of @Jürg Merlin Spaak's rollaxis but much faster and not deprecated:

b.swapaxes(0, axis)[index]
Related