来自索引列表和切片的numpy花式索引

I have a 4-dimensional array with size (d1,d2,d3,d4), and a list of indices with k entries. Each element of the indices list has 3 elements. The first is the index for the 1st dimension of the array. The second and third correspond to the start of slices for the 3rd and 4th dimension, where the slice length is a fixed number n. From the second dimension of the array I always want all elements. In the end I want to have a new array with size (k,d3,n,n). For performance reasons I want to do this without a for loop, and if possible without data-copying. The following solution works, but uses a for loop and copies the data:

d1,d2,d3,d4 = 3,4,10,10
data = np.arange(d1*d2*d3*d4).reshape((d1,d2,d3,d4))
idcs_all = [[0,4,6],
        [2,2,5]]
n = 3
k=len(idcs_all)
sub = np.zeros((k,d2,n,n))

for i, idcs in enumerate(idcs_all):
    sub[i] = data[idcs[0],:, idcs[1]:idcs[1]+n, idcs[2]:idcs[2]+n]

有没有办法对花哨的索引做同样的事情?

评论