Python中flatten用法

原创 2014年04月16日 10:20:02
  • 22667

一、用在数组

  1. >>> a = [[1,3],[2,4],[3,5]]
  2. >>> a = array(a)
  3. >>> a.flatten()
  4. array([1, 3, 2, 4, 3, 5])

二、用在列表

如果直接用flatten函数会出错

  1. >>> a = [[1,3],[2,4],[3,5]]
  2. >>> a.flatten()
  3. Traceback (most recent call last):
  4. File "<pyshell#10>", line 1, in <module>
  5. a.flatten()
  6. AttributeError: 'list' object has no attribute 'flatten'

正确的用法

  1. >>> a = [[1,3],[2,4],[3,5],["abc","def"]]
  2. >>> a1 = [y for x in a for y in x]
  3. >>> a1
  4. [1, 3, 2, 4, 3, 5, 'abc', 'def']

或者(不理解)

  1. >>> a = [[1,3],[2,4],[3,5],["abc","def"]]
  2. >>> flatten = lambda x: [y for l in x for y in flatten(l)] if type(x) is list else [x]
  3. >>> flatten(a)
  4. [1, 3, 2, 4, 3, 5, 'abc', 'def']

三、用在矩阵

  1. >>> a = [[1,3],[2,4],[3,5]]
  2. >>> a = mat(a)
  3. >>> y = a.flatten()
  4. >>> y
  5. matrix([[1, 3, 2, 4, 3, 5]])
  6. >>> y = a.flatten().A
  7. >>> y
  8. array([[1, 3, 2, 4, 3, 5]])
  9. >>> shape(y)
  10. (1, 6)
  11. >>> shape(y[0])
  12. (6,)
  13. >>> y = a.flatten().A[0]
  14. >>> y
  15. array([1, 3, 2, 4, 3, 5])

Python中flatten用法的更多相关文章

  1. python中xrange用法分析

    本文实例讲述了python中xrange用法.分享给大家供大家参考.具体如下: 先来看如下示例: >>> x=xrange(0,8) >>> print x xra ...

  2. python 中@ 的用法【转】

    这只是我的个人理解: 在Python的函数中偶尔会看到函数定义的上一行有@functionName的修饰,当解释器读到@的这样的修饰符之后,会先解析@后的内容,直接就把@下一行的函数或者类作为@后边的 ...

  3. 列表[‘hello’ , ‘python’ ,’!’ ] 用多种方法拼接,并输出’hello python !’ 以及join()在python中的用法简介

    列表[‘hello’ , ‘python’ ,’!’ ] 用多种方法拼接,并输出’hello python !’ 使用字符串链接的四种方法都可以创建 字符串拼接一共有四种方法,也可以应用到列表的拼接中 ...

  4. python中“end=”用法

    python中“end=”用法:例如print(“#”,end=" \n"),默认换行,print(“#”,end=" ")则在循环中不换行

  5. python中pkl用法

    原文连接:https://www.jianshu.com/p/2ecadebe6d13 python中pkl用法 经常遇到在Python程序运行得到了一些字符串.列表.字典等数据,想要长久的保存下来, ...

  6. Python中print用法里面% ,"%s 和 % d" 代表的意思

    Python 编程 里面% . "%s 和 % d" 代表的意思 %s,表示格化式一个对象为字符 %d,整数 "Hello, %s"%"zhang3& ...

  7. 详解python中@的用法

    python中@的用法 @是一个装饰器,针对函数,起调用传参的作用. 有修饰和被修饰的区别,‘@function'作为一个装饰器,用来修饰紧跟着的函数(可以是另一个装饰器,也可以是函数定义). 代码1 ...

  8. python中*的用法

    在python中,很多情况下会用到*,下面举一些例子来说明*的用法 1.数字计算中,*代表乘法,**代表求幂 print('2乘以3值为:%s'%(2*3)) print('2的3次方值为:%s'%( ...

  9. python中print用法

    print用法 参考文档:https://blog.csdn.net/sinat_28576553/article/details/81154912 目录 一.print()函数概述 二.变量的输出 ...

随机推荐

  1. 循环冗余校验(CRC)算法入门

    http://blog.csdn.net/liyuanbhu/article/details/7882789 前言 CRC校验(循环冗余校验)是数据通讯中最常采用的校验方式.在嵌入式软件开发中,经常要 ...

  2. Timer时钟(之一)

    using System.Timers; static void Main(string[] args) { ThreadingTimer(); DateTime a = DateTime.Now; ...

  3. ffmpeg裁剪

    http://it6655.com/2012/09/ffmpeg-9-2-html 1 configure参数 1 通用选项 在linux下进入终端,找到ffmpeg解压位置,输入如下命令: root ...

  4. js文字内容部分选中的代码封装

    var textSelect = function(o, a, b){ //o是当前对象,例如文本域对象 //a是起始位置,b是终点位置 var a = parseInt(a, 10), b = pa ...

  5. 模板BSGS(SDOI2011计算器) 模板EXBSGS

    BSGS和EXBSGS是OI中用于解决A^xΞB(mod C)的常用算法. 1.BSGS BSGS用于A,C互质的情况. 令m=sqrt(C),此时x可表示为i*m+j. 式中i和j都<=sqr ...

  6. 在springBoot的控制台打印sql语句

    在springBoot+Mybatis日志显示SQL的执行情况的最简单方法就是在properties新增: properties形式 logging.level.com.eth.wallet.mapp ...

  7. MySQL与MyBatis中的查询记录

    1.时间段查询 MySQL:select * from table where ctime >= CURDATE() and ctime <DATE_SUB(CURDATE(),INTER ...

  8. java枚举中常见的7中用法

    2016年08月11日 11:14:45 李学凯  原文链接https://blog.csdn.net/qq_27093465/article/details/52180865 JDK1.5引入了新的 ...

  9. centos 简单用户管理

    一.配置文件 /etc/passwd:存放用户信息,以“:”分割成7个部分 1.账号名称,用来对应UID: 2.早期密码存放位置,后来密码改存/etc/shadow中,以“x”代替: 3.UID,使用 ...

  10. Python基础知识点总结

    Python基础知识与常用数据类型 一.Python概述: 1.1.Python的特点: 1.Python是一门面向对象的语言,在Python中一切皆对象 2.Python是一门解释性语言 3.Pyt ...