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. js模拟支付宝发送短信验证码&&&&短信倒计时

    html <div class="pwdContent"> <div class="pwdBox"></div> <d ...

  2. Spring Cloud和Dubbo的对比

  3. 问题:执行[root@node01 hadoop-2.6.0-cdh5.14.0]# sbin/start-dfs.sh 后,namenode未启动

    执行[root@node01 hadoop-2.6.0-cdh5.14.0]# sbin/start-dfs.sh 后,namenode未启动. 解决步骤: 查看/export/servers/had ...

  4. thinkphp5中vendor的使用?

    1.创建处理数组的类ArrayList.php <?php /** * ArrayList实现类 * @author liu21st <liu21st@gmail.com> */ c ...

  5. 启动myeclipse弹窗Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance

    Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance(翻译:请允许Sub ...

  6. CentOS6.5下编译安装LAMP环境

    LAMP(Linux-Apache-MySQL-PHP)网站架构是目前国际流行的Web框架.该框架能够满足大流量.大并发量的网站需求:当然.也可以直接使用高性能的服务器.高性能的负载均衡硬件以及CDN ...

  7. 01基础数据类型——list相关操作

    #列表的创建#列表是由[]来表示的,将元素放在[]中,如lst=["aa","bb",["cc","dd"," ...

  8. LNMP中PHP服务的配置

    PHP(Hypertxt Preprocessor,超文本预处理器)是一种通用的开源脚本语言,发明于1995年,它吸取了C语言.Java语言及Perl语言的很多优点,具有开源.免费.快捷.跨平台性强. ...

  9. UVaLive 4868 Palindrometer (暴力 / 构造)

    题意: 给定一个固定长度的字符串, 字符串是一个含有前导0的数字, 问这个数字加上多少能构成一个回文字符串. 分析: 其实这题有很多种方法, 方法12是我做完后看别人代码总结的, 方法3是我当时想的一 ...

  10. 将scl分频

    多个scl后一个高脉冲,脉冲宽度一个scl脉冲. always @ ( posedge MCLK or negedge RST_N ) begin if (rClkCount == 24) //如果有 ...