据说是函数式编程的一个函数(然后也有人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的更多相关文章

  1. python中lambda,map,reduce,filter,zip函数

    函数式编程 函数式编程(Functional Programming)或者函数程序设计,又称泛函编程,是一种编程范型,它将计算机运算视为数学上的函数计算,并且避免使用程序状态以及易变对象.简单来讲,函 ...

  2. Python中的Map/Reduce

    MapReduce是一种函数式编程模型,用于大规模数据集(大于1TB)的并行运算.概念"Map(映射)"和"Reduce(归约)",是它们的主要思想,都是从函数 ...

  3. python中的map、filter、reduce函数

    三个函数比较类似,都是应用于序列的内置函数.常见的序列包括list.tuple.str.   1.map函数 map函数会根据提供的函数对指定序列做映射. map函数的定义: map(function ...

  4. Python 中的 map, reduce, zip, filter, lambda基本使用方法

    map(function, sequence[, sequence, ...] 该函数是对sequence中的每个成员调用一次function函数,如果参数有多个,则对每个sequence中对应的元素 ...

  5. 简单易懂之python 中的map,filter,reduce用法

    map(function,sequence) 把sequence中的值当参数逐个传给function,返回一个包含函数执行结果的list. 重点是结果返回一个列表,这样对返回的列表就可以干很多的活了. ...

  6. python基础之map/reduce/filter/sorted

    ---map(fun,iterable) 首先来看一看map()函数,map函数接受两个参数,第一个参数是函数的名称,第二个参数一个可迭代对象.即map(fun,iterable) map函数就是将具 ...

  7. python中的map,filter,zip函数

    map() Return an iterator that applies function to every item of iterable, yielding the results 例如: a ...

  8. [python基础知识]python内置函数map/reduce/filter

    python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法) ...

  9. python一些内建函数(map,zip,filter,reduce,yield等)

    python一些内建函数(map,zip,filter,reduce,yield等) map函数 Python实际上提供了一个内置的工具,map函数.这个函数的主要功能是对一个序列对象中的每一个元素应 ...

随机推荐

  1. climits

    <climits>头文件定义的符号常量 CHAR_MIN  char的最小值SCHAR_MAX  signed char 最大值SCHAR_MIN   signed char 最小值UCH ...

  2. Tomcat 内存溢出对应解决方式

    1.Tomcat内存溢出的原因 生产环境中Tomcat内存设置不好很容易出现内存溢出.造成内存溢出是不一样的,当然处理方式也不一样. 这里根据平时遇到的情况和相关资料进行一个总结.常见的一般会有下面三 ...

  3. Theano2.1.6-基础知识之在thenao中的求导

    来自:http://deeplearning.net/software/theano/tutorial/gradients.html Derivatives in Theano 一.计算梯度 现在,让 ...

  4. 谈对象 MVC 和 多端

    什么是对象? 我是单身狗,我没有对象:我是C程序猿,我没有对象:我是程序猿,我只会new一个对象. 言归正传,想想从一个电商网站上买一个东西,“进入首页,搜索商品,选型购买,登录下单,支付完成”,这里 ...

  5. Stem函数绘图

    stem(n,x,'filled');第三个参数是绘图的样式,filled就是填充,将圆圈填充. Stem函数绘图各种不同的绘图函数分别适用于不同的场合,使用“stem”绘制针状图最简单,从附录中提供 ...

  6. 11-cp 命令总结

  7. 常用数据库高可用和分区解决方案(1) — MySQL篇

    在本文中我们将会讨论MySQL.Oracle.MongoDB.Redis以及Oceanbase数据库,大家可能会奇怪为什么看不到有名关系型数据库MSSQL.DB2或者有名NoSQL数据库Hbase.L ...

  8. 基于jquery实现的上传图片及图片预览效果代码

    <!DOCTYPE html> <html> <head> <title>HTML5上传图片预览</title> <meta http ...

  9. Extjs 使用图标字体来美化按钮)

    1. 使用Font Awesome,下载地址http://www.bootcss.com/p/font-awesome/#icons-new 2. 把font和css目录放到 Ext的app目录下面 ...

  10. SpringMVC 参数传递

    使用@RequestParam 注解获取GET请求或POST请求提交的参数: 获取Cookie的值:使用@CookieValue : 根据不同的Web请求方法,映射到不同的处理方法:使用登陆页面作示例 ...