python之map、filter、reduce、lambda函数
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]
map(lambda x:x*x, range(5))
python之map、filter、reduce、lambda函数的更多相关文章
- python 内置函数 map filter reduce lambda
map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...
- python几个特别函数map filter reduce lambda
lambda函数也叫匿名函数,即,函数没有具体的名称.先来看一个最简单例子: def f(x): return x**2 print f(4) Python中使用lambda的话,写成这样 g = l ...
- Python中map,filter,reduce,zip的应用
事例1: l=[('main', 'router_115.236.xx.xx', [{'abc': 1}, {'dfg': 1}]), ('main', 'router_183.61.xx.xx', ...
- Python【map、reduce、filter】内置函数使用说明(转载)
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- 【转】Python 中map、reduce、filter函数
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- Python【map、reduce、filter】内置函数使用说明
题记 介绍下Python 中 map,reduce,和filter 内置函数的方法 一:map map(...) map(function, sequence[, sequence, ...]) -& ...
- python常用函数进阶(2)之map,filter,reduce,zip
Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...
- 如何在python3.3用 map filter reduce
在3.3里,如果直接使用map(), filter(), reduce(), 会出现 >>> def f(x): return x % 2 != 0 and x % 3 != 0 ...
- 数组的高阶方法map filter reduce的使用
数组中常用的高阶方法: foreach map filter reduce some every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...
- Swift map filter reduce 使用指南
转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...
随机推荐
- IE6 BUG 汇总
1.IE6双倍边距bug 当页面内有多个连续浮动时,如本页的图标列表是采用左浮动,此时设置li的左侧margin值时,在最左侧呈现双倍情况.如外边距设置为10px, 而左侧则呈现出20px,解决它的方 ...
- spring boot结合thymeleaf
1.在pom文件中加入thymeleaf相关的依赖 spring-boot-starter-thymeleaf 2.在resource文件夹下创建 template文件夹,在template文件夹中创 ...
- POI格式化Cell样式
通过一个实例演示怎样通过POI设置Excel单元格的边框.字体.颜色.大小.下划线.合并.对齐方式. Excel文件如下: Java代码 package my.excel; impor ...
- 自定义view实现侧滑菜单
自定义View public class SlidingMenu extends HorizontalScrollView { private int mScreenWidth; private in ...
- 23个MySQL常用查询语句
23个MySQL常用查询语句 一查询数值型数据: SELECT * FROM tb_name WHERE sum > 100; 查询谓词:>,=,<,<>,!=,!> ...
- Linux常用命令-入门
linux 开源安全性高 linux 和 windows 1.同时登陆多用户 2.安全 3.linux没有盘符的概念 /. / root 根目录 4.linux的文件没有扩展名 ...
- 如何用Apache TCPMon来截获SOAP消息
在WebService服务器和客户机之间会传递SOAP消息,有时我们需要得到这些消息以便调试,而Apache的TCPMon可以帮助我们做到这一点. TCPMon的下载地址在http://ws.apa ...
- pureftpd安装配置[总结]
http://www.ttlsa.com/linux/how-to-install-pureftpd/ 看了这篇文章[几个小坑]总结如下: 1.最重要的一点,代码不要复制,有些符号肉眼看不出来. ./ ...
- coursera python 学习总结
为啥要写这篇总结?早上突然想到了四个字:知行合一.实践,总结,再实践,再总结经验,积累经验,为己所用.闲话少叙,来干货: 1.目标要单一,如果想要完成课程,还要健身,还要玩玩游戏.看看电影,还学别的课 ...
- IMAP和POP3有什么区别
http://help.163.com/10/0203/13/5UJONJ4I00753VB8.html?servCode=6010237 IMAP和POP3有什么区别? POP3协议允许电子邮件 ...