flatten的用法

from numpy import *
if __name__ == '__main__':
    '''
    flatten 用于数组
    '''
    a = array([[1,2,3,4,5],[3,4,5,6,7],[1,4,5,6,74],[23,4,5,6,7]])
    print(type(a))
    print(a.flatten())
    '''
    [ 1  2  3  4  5  3  4  5  6  7  1  4  5  6 74 23  4  5  6  7]
    '''
    print(type(a.flatten()))
    '''
    flatten 用于矩阵
    '''
    b = mat([[2,3,4],[3,4,5],[4,5,6]])
    print(b)
    print(type(b))
    print(b.flatten())
    print(type(b.flatten()))
    print(b.flatten().A)
    #flatten之后矩阵变成了数组
    print(b.flatten().A[0])
    print(type(b.flatten().A[0]))
    '''
    <class 'numpy.ndarray'>
    [ 1  2  3  4  5  3  4  5  6  7  1  4  5  6 74 23  4  5  6  7]
    <class 'numpy.ndarray'>
    [[2 3 4]
     [3 4 5]
     [4 5 6]]
    <class 'numpy.matrixlib.defmatrix.matrix'>
    [[2 3 4 3 4 5 4 5 6]]
    <class 'numpy.matrixlib.defmatrix.matrix'>
    [[2 3 4 3 4 5 4 5 6]]
    [2 3 4 3 4 5 4 5 6]
    <class 'numpy.ndarray'>
    '''

flatten不能用于列表
欢迎关注我的公众号:小秋的博客 CSDN博客:https://blog.csdn.net/xiaoqiu_cr github:https://github.com/crr121 联系邮箱:rongchen633@gmail.com 有什么问题可以给我留言噢~
原文地址:https://www.cnblogs.com/flyingcr/p/10326917.html