您可以使用reshape
通过order='F'
。只要有可能,返回的数组将只是原始数组的视图,而不复制数据,例如:
a = np.arange(1, 10)
# array([1, 2, 3, 4, 5, 6, 7, 8, 9])
b = a.reshape(3, 3)
c = a.reshape(3, 3, order='F')
a[0] = 11
print(b)
#array([[ 11, 4, 7],
# [ 2, 5, 8],
# [ 3, 6, 9]])
print(c)
#array([[ 11, 4, 7],
# [ 2, 5, 8],
# [ 3, 6, 9]])
The flags
属性可用于检查数组的内存顺序和数据所有权:
print(a.flags)
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
print(b.flags)
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
print(c.flags)
C_CONTIGUOUS : False
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False