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. yum升级php版本

    查看当前 PHP 版本 1 php -v 查看当前 PHP 相关的安装包,删除之 1 2 3 4 5 yum list installed | grep php   yum remove php   ...

  2. python send email

    #!/usr/bin/python # -*- coding: UTF-8 -*- # coding:utf8 from smtplib import SMTP_SSL from email.head ...

  3. Spring Data Redis入门示例:数据序列化 (四)

    概述 RedisTemplate默认使用的是基于JDK的序列化器,所以存储在Redis的数据如果不经过相应的反序列化,看到的结果是这个样子的: 可以看到,出现了乱码,在程序层面上,不会影响程序的运行, ...

  4. Python机器学习——DBSCAN聚类

    密度聚类(Density-based Clustering)假设聚类结构能够通过样本分布的紧密程度来确定.DBSCAN是常用的密度聚类算法,它通过一组邻域参数(ϵϵ,MinPtsMinPts)来描述样 ...

  5. 专题训练——[kuangbin带你飞]最短路练习

    最短路练习 0. Til the Cows Come Home  POJ - 2387 完美的模板题 //#include<Windows.h> #include<iostream& ...

  6. [Python3网络爬虫开发实战] 7.3-Splash负载均衡配置

    用Splash做页面抓取时,如果爬取的量非常大,任务非常多,用一个Splash服务来处理的话,未免压力太大了,此时可以考虑搭建一个负载均衡器来把压力分散到各个服务器上.这相当于多台机器多个服务共同参与 ...

  7. 树莓派 - wiringPi

    wiringPi其实和BCM2835 library类似,也是通过memmap, IOmap来实现在用户空间直接操作底层寄存器 wiringPi http://wiringpi.com/ Wiring ...

  8. 如何使用 Laravel Collections 类编写神级代码

    本文首发于 如何使用 Laravel Collections 类编写神级代码,转载请注明出处. Laravel 提供了一些超赞的组件,在我看来,它是目前所有 Web 框架中提供组件支持最好的一个.它不 ...

  9. LINUX:Contos7.0 / 7.2 LAMP+R 下载安装Mysql篇

    文章来源:http://www.cnblogs.com/hello-tl/p/7569097.html 更新时间:2017-09-21 16:06 简介 LAMP+R指Linux+Apache+Mys ...

  10. Django中配置自定义日志系统