python之map、filter、reduce、lambda函数

 转  http://www.cnblogs.com/kaituorensheng/p/5300340.html

阅读目录

map

map函数根据提供的函数对指定的序列做映射,定义:
map(function, sequence[,sequence,...])--->list

例1

>>> map(lambda x:x+2, [1, 2, 3])
[3, 4, 5]
>>> map(lambda x:x+2, (1, 2, 3))
[3, 4, 5]
>>> map(lambda x:x+2, [1, 2], [1, 2])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: <lambda>() takes exactly 1 argument (2 given)

最后这个例子说lambda函数需要传2个参数(因为后面的列表是2个)

例2

>>> map(lambda x,y:x+y, [1, 2], [1, 2])
[2, 4]
>>> map(lambda x,y:x+y, [1, 2], (1,2))
[2, 4]

例3

>>> a
[{'type': 2, 'ID': 1}, {'type': 4, 'ID': 2}, {'ID': 3}]
>>> map(lambda x:x['ID'], a)
[1, 2, 3]
>>> map(lambda x:x['type'], a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
KeyError: 'type'

例子说明,如果其中的一个键不存在({'ID':3}不存在type)会报错。

例4

上面例子中只给了lambda,还可以用普通的函数

>>> def func2(x, y):
... return x+y
...
>>> map(func2, [1, 2, 3], [3, 2, 1])
[4, 4, 4]
>>>
>>> def func1(x):
... return x**2
...
>>> map(func1, [1, 2, 3])
[1, 4, 9]

例5

如果没有给定,就类似于zip函数了

>>> map(None, [1, 2, 3, 4], [1, 2, 3, 4])
[(1, 1), (2, 2), (3, 3), (4, 4)]
>>> map(None, [1, 2, 3, 4], [1, 2, 3, 4,5])
[(1, 1), (2, 2), (3, 3), (4, 4), (None, 5)]
>>> map(None, [1, 2, 3, 4], [1, 2, 3, 4,5], [1, 2, 3])
[(1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, None), (None, 5, None)]

不过与zip不同

>>> zip([1, 2, 3, 4], [1, 2, 3, 4,5], [1, 2, 3])
[(1, 1, 1), (2, 2, 2), (3, 3, 3)]

filter

filter函数对指定的序列进行过滤操作。定义:

filter(function or None, sequence) -> list, tuple, or string

例1

>>> filter(lambda x:x%2==1, [1, 2, 3])
[1, 3]
>>> filter(lambda x:x%2==1, (1, 2, 3))
(1, 3)

reduce

reduce函数会对参数序列中元素进行累积。定义:
reduce(function, sequence[, initial]) -> value

:function必须是有2个参数的函数

例1

>>> reduce(lambda x, y:x+y, [1,2,3,4])
10
>>> reduce(lambda x, y:x+y, [1,2,3,4], 10)
20

如果没有initial参数,这么算:(((1+2)+3)+4)

如果有initial参数,这么算: ((((10+1)+2)+3)+4)

lambda

编程中提到的 lambda 表达式,通常是在需要一个函数,但是又不想费神去命名一个函数的场合下使用,也就是指匿名函数

举例对比(列表中的元素平方):

>>> map(lambda x:x*x, range(5))
[0, 1, 4, 9, 16] >>> def sq(x):
... return x * x
...
>>> map(sq, range(5))
[0, 1, 4, 9, 16]
前者比后者好。因为后者多定义了一个(污染环境的)函数,尤其如果这个函数只会使用一次的话。而且第一种写法实际上更易读,因为那个映射到列表上的函数具体是要做什么,非常一目了然。如果你仔细观察自己的代码,会发现这种场景其实很常见:你在某处就真的只需要一个能做一件事情的函数而已,连它叫什么名字都无关紧要。Lambda 表达式就可以用来做这件事。

map(lambda x:x*x, range(5))
这样的写法时,你会发现自己如果能将「遍历列表,给遇到的每个元素都做某种运算」的过程从一个循环里抽象出来成为一个函数 map,然后用 lambda 表达式将这种运算作为参数传给 map 的话,考虑事情的思维层级会高出一些来,需要顾及的细节也少了一点。Python 之中,类似能用到 lambda 表达式的「高级」函数还有 reduce、filter 等等

python之map、filter、reduce、lambda函数 转的更多相关文章

  1. python 内置函数 map filter reduce lambda

    map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...

  2. python几个特别函数map filter reduce lambda

    lambda函数也叫匿名函数,即,函数没有具体的名称.先来看一个最简单例子: def f(x): return x**2 print f(4) Python中使用lambda的话,写成这样 g = l ...

  3. Python中map,filter,reduce,zip的应用

    事例1: l=[('main', 'router_115.236.xx.xx', [{'abc': 1}, {'dfg': 1}]), ('main', 'router_183.61.xx.xx', ...

  4. Python【map、reduce、filter】内置函数使用说明(转载)

    转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...

  5. 【转】Python 中map、reduce、filter函数

    转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...

  6. Python【map、reduce、filter】内置函数使用说明

    题记 介绍下Python 中 map,reduce,和filter 内置函数的方法 一:map map(...) map(function, sequence[, sequence, ...]) -& ...

  7. python常用函数进阶(2)之map,filter,reduce,zip

    Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...

  8. 如何在python3.3用 map filter reduce

    在3.3里,如果直接使用map(), filter(), reduce(), 会出现 >>> def f(x): return x % 2 != 0 and x % 3 != 0  ...

  9. 数组的高阶方法map filter reduce的使用

    数组中常用的高阶方法: foreach    map    filter    reduce    some    every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...

  10. Swift map filter reduce 使用指南

    转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...

随机推荐

  1. windows 文件权限导致的 git 问题

    windows 文件权限导致的 git 问题 在 windows 上使用 git 时,会遇到明明什么都没有改动,但是 git status 显示一堆文件被修改.这时,通过 git diff 可看到什么 ...

  2. mysql 查表失败

    我们数据库迁移,我进数据库的目录都需要拷贝什么到新的数据库才可以用,我直接拷贝的库报错了[]北京- 2016/1/26 16:07:33 mysql> use payment;Database  ...

  3. PRML读书笔记——Mathematical notation

    x, a vector, and all vectors are assumed to be column vectors. M, denote matrices. xT, a row vcetor, ...

  4. [xampp]在Crunch Bang下安装xampp1.8.3

    1.下载linux下 的xampp安装包 xampp-linux-1.8.3-5-installer.run 2.终端下, 给执行权限 sudo chmod +x ./xampp-linux-1.8. ...

  5. Using Yum Variables

    You can use and reference the following built-in variables in yum commands and in all Yum configurat ...

  6. 关于webservice大数据量传输时的压缩和解压缩

    当访问WebSerivice时,如果数据量很大,传输数据时就会很慢.为了提高速度,我们就会想到对数据进行压缩.首先我们来分析一下. 当在webserice中传输数据时,一般都采用Dataset进行数据 ...

  7. 自定义view

    这两篇文章不可错过,是最靠谱的基础文献.总的来说,如果想完全定制,就继承与于View类:如果只是在原有控件基础上拓展,那就继承TextView.Button或者LinearLayout等.接下来,就以 ...

  8. 让DIV水平和垂直居中的几种方法

    我们在设计页面的时候,经常要把DIV居中显示,而且是相对页面窗口水平和垂直方向居中显示,如让登录窗口居中显示.我们传统解决的办法是用纯CSS来让DIV居中.在本文中,我将给大家讲述如何用CSS和jQu ...

  9. Dynamics AX 2012 R3 Demo 安装与配置 - 配置安装环境 (Step 1)

    AX 2012 R3 发布后,Reinhard一直想体验一把,可是Reinhard所在的公司暂时不会升级到R3版本.这不,Reinhard就打算在个人电脑上安装下,可是安装的过程中,遇到了很多问题,R ...

  10. Dynamics AX 2012 R2 为运行失败的批处理任务设置预警

    我们主要有两种类型的系统监视:环境健康监视和性能监视. 环境健康监视一般对系统性能影响非常小,是为了提醒潜在的问题. 性能监视通常更有侵入性.监视期间,添加一个负载到环境.因此,它可以回答特定的问题或 ...