map(functioniterable...)

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. discuz !NT 3.5 论坛整合 .net 网站用户登录,退出

    using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlCont ...

  2. LoadRunner调用md5方法

    LoadRunner调用md5方法 上一篇 / 下一篇  2011-04-29 11:25:12 / 个人分类:Loadrunner 查看( 958 ) / 评论( 0 ) / 评分( 0 / 0 ) ...

  3. Nginx 限制单个IP的并发连接数及对每个连接速度(限速)

    使用Nginx限制单个IP的并发连接数能够减少一些采集程序或者DDOS的攻击. 再lnmp的nginx配置中已经添加了部分代码,但是是注释掉的,可以编辑/usr/local/nginx/conf/ng ...

  4. ThinkPHP连接主从数据库

    config.php文件设置如下: return array( 'URL_MODE'=>0,   'DB_TYPE'=>'mysql',   'DB_HOST'=>'localhos ...

  5. JavaScript_DOM编程艺术第二版[阅]

    前两年迫于项目的需要,只是拿来JQuery用到项目中,并没有实质上理解javascript(貌似其他人也是这么干的)~ 随着最近几年,得益于Nodejs, React, Vue等,javascript ...

  6. openwrt修改密码

    默认情况下root是没有密码的 需要设置密码后才能开启ssh 修改/etc/shadow文件: root:$1$wEehtjxj$YBu4quNfVUjzfv8p/PBo5.:0:0:99999:7: ...

  7. Google Code Jam 2014 Round 1 A:Problem B. Full Binary Tree

    Problem A tree is a connected graph with no cycles. A rooted tree is a tree in which one special ver ...

  8. win10 下eclipse tomcat 热部署问题?

    前言: 问题的描述: 用的环境是maven,java,tomcat,win10 tomcat server配置如下 项目发布之后,修改jsp,报错,错误详情如下: 解决办法.勾选server opti ...

  9. laravel学习之路3 数据库相关

    读写分离之多个读? 有 'host' => $readHosts[array_rand($readHosts)], 上面的好像有缓存问题php artisan config:cache ] ); ...

  10. PHP插入法排序

    /** 插入排序(Insertion Sort)的算法描述是一种简单直观的排序算法. 它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描, 找到相应位置并插入.插入排序在实现上 ...