python 中的map(), reduce(), filter
据说是函数式编程的一个函数(然后也有人tucao py不太适合干这个),在我看来算是pythonic的一种写法。
简化了我们的操作,比方我们想将list中的数字都加1,最基本的可能是编写一个函数:
In [40]: def add_one(i):
....: return i+1
....: In [41]: for i in range(1, 3):
....: print add_one(i)
....:
2
3
如果使用map就简单一些了:
In [42]: map(add_one, range(1, 3))
Out[42]: [2, 3]
其实这里还不够pythonic, 毕竟我们忘记了还有lambda这个匿名函数
In [44]: map(lambda x: x+1, range(1, 3))
Out[44]: [2, 3]
reduce的作用是对可迭代对象依次做某种操作,比方说依次相加或者乘之类的
内置的说明如下:
Help on built-in function reduce in module __builtin__: reduce(...)
reduce(function, sequence[, initial]) -> value Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5). If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.
(END)
使用方法如下:
In [18]: reduce(lambda x, y: x*y, [1, 2, 3])
Out[18]: 6
In [19]: reduce(lambda x, y: x*y, [1])
Out[19]: 1 In [20]: reduce(lambda x, y: x*y, [], 0) # 最后一个为初始值
Out[20]: 0
filter是对可迭代对象做某种过滤,使用方法和上面两个相似:
Help on built-in function filter in module __builtin__: filter(...)
filter(function or None, sequence) -> list, tuple, or string Return those items of sequence for which function(item) is true. If
function is None, return the items that are true. If sequence is a tuple
or string, return the same type, else return a list.
In [22]: filter(lambda x: x > 5, range(10))
Out[22]: [6, 7, 8, 9]
基本使用方法大致如上,可能觉得刚开始觉得用处不大,实际上用习惯了就会觉得十分顺手
比方说让大家求1到100的和,可能函数写出来也只有几行代码,但是你用reduce呢,就只有一行。
大家可以试着用这种方法算一下 1!+2!+...+100!
(发现内置的文档已经很赞了~
参考见:
http://my.oschina.net/zyzzy/blog/115096
https://eastlakeside.gitbooks.io/interpy-zh/content/Map%20&%20Filter/index.html
python 中的map(), reduce(), filter的更多相关文章
- python中lambda,map,reduce,filter,zip函数
函数式编程 函数式编程(Functional Programming)或者函数程序设计,又称泛函编程,是一种编程范型,它将计算机运算视为数学上的函数计算,并且避免使用程序状态以及易变对象.简单来讲,函 ...
- Python中的Map/Reduce
MapReduce是一种函数式编程模型,用于大规模数据集(大于1TB)的并行运算.概念"Map(映射)"和"Reduce(归约)",是它们的主要思想,都是从函数 ...
- python中的map、filter、reduce函数
三个函数比较类似,都是应用于序列的内置函数.常见的序列包括list.tuple.str. 1.map函数 map函数会根据提供的函数对指定序列做映射. map函数的定义: map(function ...
- Python 中的 map, reduce, zip, filter, lambda基本使用方法
map(function, sequence[, sequence, ...] 该函数是对sequence中的每个成员调用一次function函数,如果参数有多个,则对每个sequence中对应的元素 ...
- 简单易懂之python 中的map,filter,reduce用法
map(function,sequence) 把sequence中的值当参数逐个传给function,返回一个包含函数执行结果的list. 重点是结果返回一个列表,这样对返回的列表就可以干很多的活了. ...
- python基础之map/reduce/filter/sorted
---map(fun,iterable) 首先来看一看map()函数,map函数接受两个参数,第一个参数是函数的名称,第二个参数一个可迭代对象.即map(fun,iterable) map函数就是将具 ...
- python中的map,filter,zip函数
map() Return an iterator that applies function to every item of iterable, yielding the results 例如: a ...
- [python基础知识]python内置函数map/reduce/filter
python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法) ...
- python一些内建函数(map,zip,filter,reduce,yield等)
python一些内建函数(map,zip,filter,reduce,yield等) map函数 Python实际上提供了一个内置的工具,map函数.这个函数的主要功能是对一个序列对象中的每一个元素应 ...
随机推荐
- Servlet学习之web服务器Tomcat 详解
Web服务器是什么 Web服务器是指驻留于因特网上某种类型计算机的程序.当Web浏览器(客户端)连到服务器上并请求文件时,服务器将处理该请求并将文件发送到该浏览器上,附带的信息会告诉浏览器如何查看该文 ...
- WampServer下如何实现多域名配置(虚拟域名配置)
之前在学习跨域的时候,我写过一篇叫做WampServer下使用多端口访问的文章,默认的 localhost 采用的是 80 端口,能使用多端口访问的核心是得新建一个端口,也就是新建一个 http 服务 ...
- requirejs:性能优化-及早并行加载
为了提高页面的性能,通常情况下,我们希望资源尽可能地早地并行加载.这里有两个要点,首先是尽早,其次是并行. 通过data-main方式加载要尽可能地避免,因为它让requirejs.业务代码不必要地串 ...
- 阅读DNA-2014年读书
- SQL Server使用游标或临时表遍历数据
方法一:使用游标(此方法适用所有情况,对标结构没有特殊要求.) declare @ProductName nvarchar() declare pcurr cursor for select Prod ...
- SQL2008R2 不支持用该后端版本设计数据库关系图或表
向下不兼容. 要么安装SQL2012,要么把SQL2012数据库通过脚本转成2008
- 中间件(middlebox)
Middleboxes (also known as network functions) are systems that perform sophisticated and often state ...
- XMl各种格式转换功能代码
package com.cdv.test; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.File ...
- ios UITableview 刷新某一个cell 或 section
//一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:]; [tableview reloadSections:ind ...
- 转 浅谈算法和数据结构: 十 平衡查找树之B树
前面讲解了平衡查找树中的2-3树以及其实现红黑树.2-3树种,一个节点最多有2个key,而红黑树则使用染色的方式来标识这两个key. 维基百科对B树的定义为"在计算机科学中,B树(B-tre ...