问题描述
似乎您正在考虑考虑使用一定数量的cols来形成块,然后获取每个块中的元素,然后移至下一个。因此,考虑到这一点,这是一种方法-
In [148]: a
Out[148]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [149]: ncols = 2 # no. of cols to be considered for each block
In [150]: a.reshape(a.shape[0],-1,ncols).swapaxes(0,1).ravel()
Out[150]: array([ 0, 1, 4, 5, 8, 9, 2, 3, 6, 7, 10, 11])
后面的动机将在中详细讨论this post
。
此外,要保留2D格式-
In [27]: a.reshape(a.shape[0],-1,ncols).swapaxes(0,1).reshape(-1,ncols)
Out[27]:
array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[ 2, 3],
[ 6, 7],
[10, 11]])
并以 直观的 3D阵列格式显示它-
In [28]: a.reshape(a.shape[0],-1,ncols).swapaxes(0,1)
Out[28]:
array([[[ 0, 1],
[ 4, 5],
[ 8, 9]],
[[ 2, 3],
[ 6, 7],
[10, 11]]])
解决方法
有什么简单的方法可以展平
import numpy
np.arange(12).reshape(3,4)
Out[]:
array([[ 0,1,2,3],[ 4,5,6,7],[ 8,9,10,11]])
进入
array([ 0,4,8,3,7,11])