map(function, iterable, ...)

map()函数接收两个参数,一个是函数,一个是可迭代的对象,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回。

基本等价于 [f(x) for x in interable]

>>> map(lambda x:x*2,xrange(4))
[0, 2, 4, 6]
>>> [x*2 for x in xrange(4)]
[0, 2, 4, 6]
>>> l=["far","foo","bar"]
>>> map(lambda x:x.upper(),l)
['FAR', 'FOO', 'BAR']
>>> map(lambda x: "f" in x,l)
[True, True, False]
>>> filter(lambda x:"f" in x,l)
['far', 'foo']
>>> map(lambda x:x.upper(),filter(lambda x:"f" in x,l))
['FAR', 'FOO']
#map 并行

def formular_1(x1,x2,x3):
return 100*x1+20*x2+x3 a=[1,2,3]
b=[-1,-2,-3]
c=[1.0,2.5,3.5] map(formular_1,a,b,c) [81.0, 162.5, 243.5]
#当fun为None,就类似zip
map(None,a,b,c)
[(1, -1, 1.0), (2, -2, 2.5), (3, -3, 3.5)]

reduce( func, seq[, init] )

reduce函数即为化简,它是这样一个过程:每次迭代,将上一次的迭代结果(第一次时为init的元素,如没有init则为seq的第一个元素)与下一个元素一同执行一个二元的func函数。在reduce函数中,init是可选的,如果使用,则作为第一次迭代的第一个元素使用。

这里面函数必须有俩个参数。

reduce(lambda x,y :x*y,xrange(1,10))
362880
reduce(lambda x,y :x*y,xrange(1,10),0.5)
181440.0

filter( func, seq )

该内建函数的作用相当于一个筛子。func函数是一个布尔函数,filter()调用这个布尔函数,将每个seq中的元素依次过一遍筛子,选出使func返回值是Ture的元素的序列。

scores=[49,59,50,66,89,100]
def score_filter(score):
return score>=80
#
result=[]
for score in scores:
if score_filter(score):
result.append(score) [89, 100] #2 filter(score_filter,scores) [89, 100] #3 list comprehension 列表表达式 [score for score in scores if score>=80] [89, 100]
filter(lambda s:s and s.strip(),["A","",None,"C"])

['A', 'C']

apply(func [, args [, kwargs ]])

函数用于当函数参数已经存在于一个元组或字典中时,间接地调用函数。args是一个包含将要提供给函数的按位置传递的参数的元组。如果省略了args,任 何参数都不会被传递,kwargs是一个包含关键字参数的字典。apply()的返回值就是func()的返回值,apply()的元素参数是有序的,元素的顺序必须和func()形式参数的顺序一致

然而,由于目前的Python中,已经可以在函数调用中使用非关键字参数和关键字参数作为可变长参数调用,apply()已经被从Python1.6开始被摒弃淘汰。因此,此处仅对该函数进行一个大致的介绍,而不具体深入,也不应在编程中再使用该函数

def say():
print 'say in' apply(say) say in def fun_add(x):
print str(x) apply(fun_add,("")) 1 def say(a, b):
print a, b apply(say,("hello", "python")) hello python

Python map,reduce,filter,apply的更多相关文章

  1. Python map/reduce/filter/sorted函数以及匿名函数

    1. map() 函数的功能: map(f, [x1,x2,x3]) = [f(x1), f(x2), f(x3)] def f(x): return x*x a = map(f, [1, 2, 3, ...

  2. python map, reduce,filter 使用

    参考python built-on function: http://docs.python.org/2.7/library/functions.html?highlight=map%20reduce ...

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

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

  4. Demo of Python "Map Reduce Filter"

    Here I share with you a demo for python map, reduce and filter functional programming thatowned by m ...

  5. Python学习:函数式编程(lambda, map() ,reduce() ,filter())

    1. lambda: Python 支持用lambda对简单的功能定义“行内函数” 2.map() : 3.reduce() : 4.filter() : map() ,reduce() , filt ...

  6. python 函数式编程之lambda( ), map( ), reduce( ), filter( )

    lambda( ), map( ), reduce( ), filter( ) 1. lambda( )主要用于“行内函数”: f = lambda x : x + 2 #定义函数f(x)=x+2 g ...

  7. python--函数式编程 (高阶函数(map , reduce ,filter,sorted),匿名函数(lambda))

    1.1函数式编程 面向过程编程:我们通过把大段代码拆成函数,通过一层一层的函数,可以把复杂的任务分解成简单的任务,这种一步一步的分解可以称之为面向过程的程序设计.函数就是面向过程的程序设计的基本单元. ...

  8. map/reduce/filter/lambda

    Python内建了map()/reduce()/filter()函数. map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把结果作为新的It ...

  9. Python-函数式编程-map reduce filter lambda 三元表达式 闭包

    lambda 匿名函数,核心是作为算子,处理逻辑只有一行但具有函数的特性,核心用于函数式编程中 三元运算符 其实本质上是if分支的简化版,满足条件返回 if 前面的值,不满足条件返回 else后面的值 ...

随机推荐

  1. KeePass 不能复制账号密码

    最近一段时间发现 KeePass 不能复制账号密码了, 程序也没有任何错误提示, 没有办法定位原因, 这个就难办了. 网上搜索后发现这个帖子 Can't paste in gnome-terminal ...

  2. GMGC记实(上篇)

    24日和25日參加了2014年GMGC大会,整体感觉今年的大会比前2届大会办的更符合听众的需求.由于今年的大会开设了开发人员训练营的分会场.在成都这样一个CP占主流的IT圈中非常有意义.另一点就是在会 ...

  3. 每天一个JavaScript实例-展示设置和获取CSS样式设置

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  4. 【DB2】db2命令Export与Import

    环境准备 1.新建表 qinys@Linux:~> db2 "create table tb1(id int,dt timestamp,name varchar(100))" ...

  5. TortoiseGit 连接oschina不用每次输入username和password的方法

    每次git clone 和push 都要输入username和password.尽管安全.但在本机上每次都输有些麻烦,怎样记住username和password呢? 在网上看了各种方法,太杂,非常多可 ...

  6. linux本地文件上传之RZ/SZ和sftp

    将本地的文件上传到服务器或者从服务器上下载文件到本地,rz / sz命令很方便的帮我们实现了这个功能,但是很多Linux系统初始并没有这两个命令. 1.软件安装 (1)编译安装 root 账号登陆后, ...

  7. AngularJS路由 $state服务、路由事件、获取路由参数

    1 ui-sref.$state.go 的区别 ui-sref 一般使用在 <a>...</a>: <a ui-sref="message-list" ...

  8. iOS端App的icon和Launch Image规格实时更新

    启动影像 : iPhone :320 x 480 640 x 960 640*1136 750*1334 1242*2208  iPad :768 x 1004 1536 x 2008 APP图标: ...

  9. Frogger - poj 2253 (Dijkstra)

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28802   Accepted: 9353 Description Fr ...

  10. Stacks of Flapjacks(栈)

     Stacks of Flapjacks  Background Stacks and Queues are often considered the bread and butter of data ...