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. webgl 的空间变换(下):空间变换

    在网上看了很多关于在三维世界中怎么把一个顶点经过一步步变化,最终呈现在我们的屏幕上的. 其实很多博客或者书籍已经讲的很清楚了,那为什么我还要特别再写一次博客来阐述自己观点呢?(这里只针对那些学习web ...

  2. CAD插入背景图片(网页版)

    把图片作为背景图片可见但是不能编辑操作. 主要用到函数说明: _DMxDrawX::DrawImageToBackground 绘光栅图到背景.详细说明如下: 参数 说明 BSTR sFileName ...

  3. UTF-8,UTF-16

    UTF是 Unicode Translation Format,即把Unicode转做某种格式的意思. 在Unicode基本多文种平面定义的字符(无论是拉丁字母.汉字或其他文字或符号),一律使用2字节 ...

  4. ajax中的json和jsonp详解

    出现的问题: 花了点时间研究ajax中的json和jsonp的原理,这里记录一下.以前一直在使用ajax调用数据,但是从来没有遇到跨域问题,也从来没有注意过json和jsonp的区别,总是一通乱用.但 ...

  5. vue 轮播插件使用

    <template> <div> <Swiper ref="swiper" v-if="list.length > 0" : ...

  6. Go:二分查找

    package main import "fmt" func BinarySearch(arr *[5]int, leftIndex int, rightIndex int, fi ...

  7. CURL PHP模拟浏览器get和post

    模拟浏览器get和post数据需要经常用到的类, 在这里收藏了几个不错的方法 方法一 <?php define ( 'IS_PROXY', true ); //是否启用代理 /* cookie文 ...

  8. 基于flask的网页聊天室(一)

    基于flask的网页聊天室(一) 基本目标 基于flask实现的web聊天室,具有基本的登录注册,多人发送消息,接受消息 扩展目标 除基本目标外添加当前在线人数,消息回复,markdown支持,历史消 ...

  9. (贪心) Vacations 求休息的最少天数

    Description Vasya has n days of vacations! So he decided to improve his IT skills and do sport. Vasy ...

  10. Reactor Cooling(无源汇有上下界网络流)

    194. Reactor Cooling time limit per test: 0.5 sec. memory limit per test: 65536 KB input: standard o ...