在python中访问n维矩阵的第j维

时间:2018-07-17 14:02:36

标签: python numpy multidimensional-array indexing dimensions

1 个答案:

答案 0 :(得分:1)

从切片构造索引元组:

In [88]: a = [1,0,4]; b = [4,1,None]
In [89]: idx = [slice(i,j) for i,j in zip(a,b)]
In [90]: idx
Out[90]: [slice(1, 4, None), slice(0, 1, None), slice(4, None, None)]
In [91]: arr = np.arange(5*3*7).reshape(5,3,7)
In [92]: arr[tuple(idx)]
Out[92]: 
array([[[25, 26, 27]],

       [[46, 47, 48]],

       [[67, 68, 69]]])
In [93]: _.shape
Out[93]: (3, 1, 3)