Python -- map, Lambda, filter and reduce
map(func, seq)
对seq中的每一个元素,调用func并返回结果。典型的应用是使用lambda函数。
>>> def square(x):
return x**2 >>> square = map(square,range(10))
>>> print(square)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>>
>>> names = ['Anne', 'Amy', 'Bob', 'David', 'Carrie', 'Barbara', 'Zach']
>>> lengths = map(len,names)
>>> print(lengths)
[4, 3, 3, 5, 6, 7, 4]
>>>
lambda
使用lambda关键创建匿名函数
>>> print((lambda x:x**2)(5))
25
>>> print((lambda x,y: x*y)(3,4))
12
>>> print((lambda x: x.startswith('B'))('Bob'))
True
>>> print((lambda x:x.startswith('B'))('Robert'))
False
>>> incr = lambda input:input+1
>>> print(incr(4))
5
>>>
lambda和map一起使用
>>> squares = map(lambda x:x**2,range(10))
>>> print(squares)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>>
filter(func or None, seq)
对seq中的每一个元素,调用func并返回那些使func为true的seq中的项。
也可以在filter()中使用lambda
>>> squares = map(lambda x:x**2,range(10))
>>> special_squares = filter(lambda x:x>5 and x<50,squares)
>>> print(special_squares)
[9, 16, 25, 36, 49]
>>>
>>> names = ['Anne', 'Amy', 'Bob', 'David', 'Carrie', 'Barbara', 'Zach']
>>> b_names = filter(lambda s:s.startswith('B'),names)
>>> print(b_names)
['Bob', 'Barbara']
>>>
reduce(func, seq)
对seq中的每一个元素,调用func并返回单个结果
如果seq = [ s1, s2, s3, ... , sn ],按照以下方式执行:
-先取出seq中的两个元素s1,s2调用func(s1,s2)
-将上一步的结果和seq中的下个元素一起调用func,相当于func(func(s1,s2),s3)
-按照上面的步骤继续调用下去
-最终返回单个值作为reduce()的返回值
>>> reduce(lambda x,y:x+y, [47,11,42,13])
113
>>>
计算过程如下:

>>> f = lambda a,b:a if(a>b) else b
>>> reduce(f,[47,11,42,102,13])
102
>>>
>>> reduce(lambda x,y:x+y,range(1,101))
5050
>>>
Python -- map, Lambda, filter and reduce的更多相关文章
- python之map、filter、reduce、lambda函数 转
python之map.filter.reduce.lambda函数 转 http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...
- Python 之map、filter、reduce
MAP 1.Python中的map().filter().reduce() 这三个是应用于序列的内置函数,这个序列包括list.tuple.str. 格式: 1>map(func,swq1[,s ...
- map、filter、reduce、lambda
一.map.filter.reduce map(fuction , iterable) 映射 对可迭代对象中的每一项,使用函数去改变 filter(function, iterable) 过滤 可迭代 ...
- python的map,filter,reduce学习
python2,python3中map,filter,reduce区别: 1,在python2 中,map,filter,reduce函数是直接输出结果. 2,在python3中做了些修改,输出前需要 ...
- C#数组的Map、Filter、Reduce操作
在Javascript.Python等语言里,Map.Filter和Reduce是数组的常用方法,可以让你在实现一些数组操作时告别循环,具有很高的实用价值.它们三个的意义大家应该都清楚,有一个十分形象 ...
- Swift函数编程之Map、Filter、Reduce
在Swift语言中使用Map.Filter.Reduce对Array.Dictionary等集合类型(collection type)进行操作可能对一部分人来说还不是那么的习惯.对于没有接触过函数式编 ...
- ES6 数组遍历方法的实战用法总结(forEach,every,some,map,filter,reduce,reduceRight,indexOf,lastIndexOf)
目录 forEach every some map filter reduce && reduceRight indexOf lastIndexOf 前言 ES6原生语法中提供了非常多 ...
- 辅助函数和高阶函数 map、filter、reduce
辅助函数和高阶函数 map.filter.reduce: 一.辅助函数:(1-1)响应式函数 (数组更新检测): push() pop() shift() unshift() ...
- python之map、filter、reduce、lambda函数
map map函数根据提供的函数对指定的序列做映射,定义:map(function, sequence[,sequence,...])--->list 例1 >>> map(l ...
随机推荐
- VC++中多字节字符集和Unicode之间的互换
在Visual C++.NET中,默认的字符集是Unicode,这和Windows默认的字符集是一致的,不过在老的VC6.0等工程中,默认的字符集形式是多字节字符集(MBCS:Multi-Byte C ...
- .Net C# 5.0 规范:迭代器
本文内容 枚举器 enumerator 接口 - IEnumerator 可枚举 enumerable 接口 - IEnumerable 产生类型 yield type 枚举器 enumerator ...
- Android Studio Gradle优化方法
第一步:打开AS安装所在的位置,用记事本打开"红色框"选中的文件. 如图: 第二步:打开"studio64.exe.vmoptions"文件后修改里面的值,修改 ...
- Mybatis 自定义SqlSessionFactoryBean扫描通配符typeAliasesPackage
typeAliasesPackage 默认只能扫描某一个路径下,或以逗号等分割的 几个路径下的内容,不支持通配符和正则,采用重写的方式解决 package com.xxxx.xxx.util.comm ...
- LintCode: Combination Sum
一个数可以使用多次 图: 节点:x(当前的和,当前要考虑的数a[i]) 边:x-> y1(当前的和,下一个要考虑的数a[i+1]) y2(当前的和+a[i],下一个要考虑的数a[i+1]) BF ...
- Centos7中安装Python3
环境:CentOS 7 1. 安装依赖环境 # yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-dev ...
- Visual SVN的安装
作为一个程序开发人员,就算自己一个人写程序,也应该有一个SVN版本控制系统,以便对开发代码进行有效的管理.今天我就介绍一个在Windows环境下简单快速搭建SVN服务器的方法. 通常的SVN服务器是搭 ...
- JDK5.0 特性线程 同步装置之CountDownLatch 同步装置之CyclicBarrier 线程 BlockingQueue
来自:http://www.cnblogs.com/taven/category/475298.html import java.util.concurrent.CountDownLatch; imp ...
- https调试
我们知道https通信在开始时会发送一个METHOD为CONNECT的请求,让服务器将证书以及相关的信息返回给浏览器,在没有得到这些信息之前,浏览器是不会信任服务器发来的任何数据的.So现在我们要让F ...
- http2.0 特性
1.HTTP 2.0将只用于https://网址,而 http://网址将继续使用HTTP/1. 查看http协议(chrome F12) 2.异步连接多路复用 HTTP2.0 把消息分解为独立帧,交 ...