NumPy 数组操作 numpy.ndarray.flatten
此函数返回将数组折叠到一维的副本。该函数接受以下参数。
ndarray.flatten(order)
其中,
序号 | 参数和描述 |
---|---|
1 | order ‘C’ – 行优先 (默认). ‘F’ – 列优先. ‘A’ – 如果a在内存中以Fortran连续方式存储,则是列优先, 否则是行优先. ‘K’ – 按照内存中元素出现的顺序展开a |
示例
import numpy as np
a = np.arange(8).reshape(2,4)
print 'The original array is:'
print a
print '\n'
# default is column-major
print 'The flattened array is:'
print a.flatten()
print '\n'
print 'The flattened array in F-style ordering:'
print a.flatten(order = 'F')
上述程序的输出如下:
The original array is:
[[0 1 2 3]
[4 5 6 7]]
The flattened array is:
[0 1 2 3 4 5 6 7]
The flattened array in F-style ordering:
[0 4 1 5 2 6 3 7]