Python Numpy ndarray.flatten()
numpy.ndarray.flatten()函数返回一个折叠成一维的数组副本。
语法: numpy.ndarray.flatten(order=’C’)
参数 :
order : [{‘C’, ‘F’, ‘A’, ‘K’}, optional] ‘C’表示以行为主(C风格)的顺序进行平移。’F’表示以列为主(Fortran-风格)的顺序进行平移。A’意味着如果a在内存中是Fortran连续的,就以列为主的顺序进行平铺,否则以行为主的顺序进行平铺。K’意味着按照元素在内存中出现的顺序来平铺a。默认是’C’。
返回 : [ndarray] 一个输入数组的副本,被压扁为一维。
代码#1:
# Python program explaining
# numpy.ndarray.flatten() function
# importing numpy as geek
import numpy as geek
arr = geek.array([[5, 6], [7, 8]])
gfg = arr.flatten()
print( gfg )
输出 :
[5 6 7 8]
代码#2:
# Python program explaining
# numpy.ndarray.flatten() function
# importing numpy as geek
import numpy as geek
arr = geek.array([[5, 6], [7, 8]])
gfg = arr.flatten('F')
print( gfg )
输出 :
[5 6 7 8]