在数组索引中自动切换维度(python)

在数组索引中自动切换维度(python),python,arrays,indexing,Python,Arrays,Indexing,我想用Python自动化下面的操作。给定一个n维np.数组,例如 ar = np.array([[[ 8, 10], [ 9, 11], [10, 12]], [[ 9, 11], [10, 12], [11, 13]]]) dim_names = ['A','B','C'] 带有尺寸名称中每个尺寸的名称,例如 ar = np.arra

我想用Python自动化下面的操作。给定一个n维
np.数组
,例如

ar = np.array([[[ 8, 10],
                [ 9, 11],
                [10, 12]],
               [[ 9, 11],
                [10, 12],
                [11, 13]]])
dim_names = ['A','B','C']
带有
尺寸名称中每个尺寸的名称,例如

ar = np.array([[[ 8, 10],
                [ 9, 11],
                [10, 12]],
               [[ 9, 11],
                [10, 12],
                [11, 13]]])
dim_names = ['A','B','C']
选择任何给定尺寸的所有边缘,例如

ind  = dim_names.index('B')

for i in range(ar.shape[ind]):
    print(ar[:,i,:])

但是根据
ind
索引必须改变,例如对于
ind=0
我们需要
ar[i,:,:]
或者对于
ind=2
ar[:,:,i]
。此外,它应该适用于任意数量的维度数组。

可能有更好的方法解决您的问题,但对于问题,您可以在循环中动态构造索引,即指定索引和切片的所需维度

沿第一维度切片:

ind = 0
​
for i in range(ar.shape[ind]):
    idx = [slice(None)] * ar.ndim             # inintialize the index with the same length 
                                              # as the dimensions
    idx[ind] = i                              # modify the index of the desired dimension
    print(ar[tuple(idx)])

#[[ 8 10]
# [ 9 11]
# [10 12]]
#[[ 9 11]
# [10 12]
# [11 13]]
ind = 1
​
for i in range(ar.shape[ind]):
    idx = [slice(None)] * ar.ndim
    idx[ind] = i
    print(ar[tuple(idx)])

#[[ 8 10]
# [ 9 11]]
#[[ 9 11]
# [10 12]]
#[[10 12]
# [11 13]]
ind = 2
​
for i in range(ar.shape[ind]):
    idx = [slice(None)] * ar.ndim
    idx[ind] = i
    print(ar[tuple(idx)])

#[[ 8  9 10]
# [ 9 10 11]]
#[[10 11 12]
# [11 12 13]]
沿第二维度切片:

ind = 0
​
for i in range(ar.shape[ind]):
    idx = [slice(None)] * ar.ndim             # inintialize the index with the same length 
                                              # as the dimensions
    idx[ind] = i                              # modify the index of the desired dimension
    print(ar[tuple(idx)])

#[[ 8 10]
# [ 9 11]
# [10 12]]
#[[ 9 11]
# [10 12]
# [11 13]]
ind = 1
​
for i in range(ar.shape[ind]):
    idx = [slice(None)] * ar.ndim
    idx[ind] = i
    print(ar[tuple(idx)])

#[[ 8 10]
# [ 9 11]]
#[[ 9 11]
# [10 12]]
#[[10 12]
# [11 13]]
ind = 2
​
for i in range(ar.shape[ind]):
    idx = [slice(None)] * ar.ndim
    idx[ind] = i
    print(ar[tuple(idx)])

#[[ 8  9 10]
# [ 9 10 11]]
#[[10 11 12]
# [11 12 13]]
沿三维切片:

ind = 0
​
for i in range(ar.shape[ind]):
    idx = [slice(None)] * ar.ndim             # inintialize the index with the same length 
                                              # as the dimensions
    idx[ind] = i                              # modify the index of the desired dimension
    print(ar[tuple(idx)])

#[[ 8 10]
# [ 9 11]
# [10 12]]
#[[ 9 11]
# [10 12]
# [11 13]]
ind = 1
​
for i in range(ar.shape[ind]):
    idx = [slice(None)] * ar.ndim
    idx[ind] = i
    print(ar[tuple(idx)])

#[[ 8 10]
# [ 9 11]]
#[[ 9 11]
# [10 12]]
#[[10 12]
# [11 13]]
ind = 2
​
for i in range(ar.shape[ind]):
    idx = [slice(None)] * ar.ndim
    idx[ind] = i
    print(ar[tuple(idx)])

#[[ 8  9 10]
# [ 9 10 11]]
#[[10 11 12]
# [11 12 13]]