zoukankan      html  css  js  c++  java
  • 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 有什么问题可以给我留言噢~
  • 相关阅读:
    vijos 1379 字符串的展开
    BZOJ 4597 随机序列
    BZOJ 2303 方格染色
    BZOJ 2654 tree
    BZOJ 4198 荷马史诗
    BZOJ 1555 KD之死
    不重复数字
    Rails
    Train Problem I
    Key Set HDU
  • 原文地址:https://www.cnblogs.com/flyingcr/p/10326917.html
Copyright © 2011-2022 走看看