Sam*_*uel 4 python arrays numpy reshape
我有一个像下面的数组,
from numpy import *
a=array([1,2,3,4,5,6,7,8,9])
Run Code Online (Sandbox Code Playgroud)
我希望得到如下结果
[[1,4,7],[2,5,8],[3,6,9]]
Run Code Online (Sandbox Code Playgroud)
因为我有一个大阵容.所以我需要一种有效的方法来做到这一点.并且最好将其重新整形.
您可以使用reshape并将 order 参数更改为 FORTRAN (column-major) order:
a.reshape((3,3),order='F')
Run Code Online (Sandbox Code Playgroud)
正如@ atomh33ls所提出的,你可以使用重塑传递order='F',并且"如果可能",返回的数组将只是原始视图的视图,而不会复制数据,例如:
a=array([1,2,3,4,5,6,7,8,9])
b = a.reshape(3,3, order='F')
a[0] = 11
print b
#array([[ 1, 4, 7],
# [ 2, 5, 8],
# [ 3, 6, 9]])
Run Code Online (Sandbox Code Playgroud)