flatten是numpy.ndarray.flatten的一个函数,其官方文档是这样描述的:
ndarray.flatten(order='C')
Return a copy of the array collapsed into one dimension.
Parameters:
order : {‘C', ‘F', ‘A', ‘K'}, optional
‘C' means to flatten in row-major (C-style) order. ‘F' means to flatten in column-major (Fortran- style) order. ‘A' means to flatten in column-major order if a is Fortran contiguous in memory, row-major order otherwise. ‘K' means to flatten a in the order the elements occur in memory. The default is ‘C'.
Returns:y : ndarray
A copy of the input array, flattened to one dimension.
即返回一个折叠成一维的数组。但是该函数只能适用于numpy对象,即array或者mat,普通的list列表是不行的。
例子:
1、用于array对象
from numpy import * >>>a=array([[1,2],[3,4],[5,6]]) ###此时a是一个array对象>>>aarray([[1,2],[3,4],[5,6]])>>>a.flatten()array([1,2,3,4,5,6])2、用于mat对象
3、但是该方法不能用于list对象
想要list达到同样的效果可以使用列表表达式:
4、用在矩阵
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。