Python: Index an array using the colon operator in an arbitrary dimension
问题 I have a numpy nd array. A simplified version of my task is to take a vector from along each axis. To illustrate: import numpy x = numpy.array(range(24)).reshape((2,3,4)) x0 = x[0,0,:] x1 = x[0,:,0] x2 = x[:,0,0] However I do not necessarily know the number of dimensions x will have. So the challenge is how to place the colon : indexing operator in a variable location. An example of what such syntax could look like: n = x.ndim ind = list(np.zeros(n)) dim = 0 ind[dim] = ':' y = x[ind] or y =