得到一张脸:
def get_face(M,dim,front_side):
if front_side:
side = 0
else:
side = -1
index = tuple(side if i == dim else slice(None) for i in range(M.ndim))
return M[index]
要添加面部(未经测试):
def add_face(M,new_face,front_side):
#assume sizes match up correctly
if front_side:
return np.concatenate((new_face,M),dim)
else:
return np.concatenate((M,new_face),dim)
要删除一张脸:
def remove_face(M,front_side):
if front_side:
dim_slice = slice(1,None)
else:
dim_slice = slice(None,-1)
index = tuple(dim_slice if i == dim else slice(None) for i in range(M.ndim))
return M[index]
迭代所有面孔:
def iter_faces(M):
for dim in range(M.ndim):
for front_side in (True,False):
yield get_face(M,front_side)
一些快速测试:
In [18]: M = np.arange(27).reshape((3,3,3))
In [19]: for face in iter_faces(M): print face
[[0 1 2]
[3 4 5]
[6 7 8]]
[[18 19 20]
[21 22 23]
[24 25 26]]
[[ 0 1 2]
[ 9 10 11]
[18 19 20]]
[[ 6 7 8]
[15 16 17]
[24 25 26]]
[[ 0 3 6]
[ 9 12 15]
[18 21 24]]
[[ 2 5 8]
[11 14 17]
[20 23 26]]