MAP

1.Python中的map()、filter()、reduce()
这三个是应用于序列的内置函数,这个序列包括list、tuple、str.
格式:
1>map(func,swq1[,seq2,...])
第一个参数接受一个函数名,后面的参数接受一个或多个可迭代的序列,返回的是一个集合。
Python函数编程中map()函数是将func作用域seq中的每一个元素,并将所有的调用的结果作为一个list返回。如果func为None,作用同zip()。(变为一个含有几个元组的列表)
另一个解释,function可以理解为是一个一对一或者多对一的函数,map作用是以参数序列中的每一个元素调用function函数,返回包含每次function函数返回值的list。
1.1>比如要对一个序列中的每个元素进行平方运算:
map(lamda x:x**2,[1,2,3,4,5])

'''[1,4,9,16,25]'''

        1.2>在参数存在多个序列时,会依次每个序列中相同位置的元素做参数调用function函数。比如要对两个序列中的元素依次求和。
map(lambda x,y:x+y,[1,3,5,7,9],[2,4,6,8,10])

'''[3,7,11,15,19]'''

        注意:function函数的参数数量,要和map中提供的集合数量相匹配,如果集合长度不等,会以最小长度对所有集合进行截取。
1.3>当函数为None是,操作和zip相似
map(None,[1,3,5,7,9],[2,4,6,8,10])

'''[(1,2),(3,4),(5,6),(7,8),(9,10)]'''

filter函数

filter函数会对序列执行过滤操作。
1.定义
filter(function or None,sequence)------>list,tuple ,or string
function是一个谓语函数,接受一个参数,返回布尔值TRUE或FALSE。
filter函数会对序列参数sequence中的每个参数调用function函数,最后返回的结果为TRUE的元素,返回值的类型和参数sequence的类型相同。
2.例子
def is_even(x):
return x&1 != 0
filter(is_even,[1,2,3,4,5,6,7,8,9,10])
结果:

'''[1,3,5,7,9]'''

reduce 函数

1.定义:reduce(function,sequence[,initial]) --->value
function参数一个有两个参数的函数,reduce依次从sequence中去一个元素,和上一次调用function的结果做参数在此调用function。
第一次调用function时,如果提供initial参数,会以sequence中第一个元素和initial作为参数调用function,否则会以序列sequence中前两个元素做参数调用function。
注意:function函数不能为None
reduce(lambda x,y:x+y,[2,3,4,5,6],1)

'''21 ((((((1+2)+3)+4)+5)+6)'''

reduce(lambda x,y:x+y,[2,3,4,5,6])

'''20'''

Python 之map、filter、reduce的更多相关文章

  1. Python中map,filter,reduce,zip的应用

    事例1: l=[('main', 'router_115.236.xx.xx', [{'abc': 1}, {'dfg': 1}]), ('main', 'router_183.61.xx.xx', ...

  2. Python【map、reduce、filter】内置函数使用说明(转载)

    转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...

  3. 【转】Python 中map、reduce、filter函数

    转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...

  4. python常用函数进阶(2)之map,filter,reduce,zip

    Basic Python : Map, Filter, Reduce, Zip 1-Map() 1.1 Syntax # fun : a function applying to the iterab ...

  5. Python【map、reduce、filter】内置函数使用说明

    题记 介绍下Python 中 map,reduce,和filter 内置函数的方法 一:map map(...) map(function, sequence[, sequence, ...]) -& ...

  6. python 内置函数 map filter reduce lambda

    map(函数名,可遍历迭代的对象) # 列组元素全加 10 # map(需要做什么的函数,遍历迭代对象)函数 map()遍历序列得到一个列表,列表的序号和个数和原来一样 l = [2,3,4,5,6, ...

  7. 如何在python3.3用 map filter reduce

    在3.3里,如果直接使用map(), filter(), reduce(), 会出现 >>> def f(x): return x % 2 != 0 and x % 3 != 0  ...

  8. Swift map filter reduce 使用指南

    转载:https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/ Using map, filter or reduce to ope ...

  9. 数组的高阶方法map filter reduce的使用

    数组中常用的高阶方法: foreach    map    filter    reduce    some    every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...

  10. Python中Lambda, filter, reduce and map 的区别

    Lambda, filter, reduce and map Lambda Operator Some like it, others hate it and many are afraid of t ...

随机推荐

  1. A Graph Partitioning Game Theoretical Approach for the VNF Service Chaining Problem

    文章名称:A Graph Partitioning Game Theoretical Approach for the VNF Service Chaining Problem 发表时间:2017 期 ...

  2. esnext:Function.prototype.toString 终于有规范了

    从 ES1 到 ES5 的这 14 年时间里,Function.prototype.toString 的规范一字未变: An implementation-dependent representati ...

  3. webpack构建Vue工程

    先开始webpack基本构建   创建一个工程目录 vue-structure mkdir vue-structure && cd vue-structure   安装webpack ...

  4. MySQL入门(参考官网)

    目录 一.登陆和退出 1. 远程主机 2. 登陆本机 3. 退出mysql 二.输入查询 三.创建和使用数据库 3.1 创建和选择数据库 3.2 创建表 3.3 将数据加载到表中 3.4 操作表格 3 ...

  5. alembic使用

    前言 alembic是SQLAlchemy作者编写的控制 model 版本的模块,配合SQLAlchemy使用更佳 正文 安装 pip install alembic alembic是可以在DOS中执 ...

  6. java控台输入

    import java.util.Scanner;//访问util包的Scanner(控台输入) public class HelloWorld {public static void main(St ...

  7. 资本寒冬下的android面经

    在2018年10月初,公司倒闭,无奈走上找工作的道路,不想自己平时图安逸,不思进取,再次找工作才发现,android行业也不是站在风口上,猪也能吹上天的世道了.作为技术小菜的我,再找工作那几个月真是战 ...

  8. python学习记录20181207

    1.python中函数指针的用法 直接把函数名赋值给变量,不需要加上()和形参 如: def add(num1,num2): return num1+num2 fun = add 2.打印输出列表需要 ...

  9. Xcode注释快捷键和Alfred 快捷键冲突解决方案

    在Alfred 中的Features -> File Search ->Navigation ->Previous Path 中的快捷方式改掉就可以了

  10. ng2-translate 国际化中 配置文件添加变量

    1. <li> {{ 'Withdrawmoney.tipsP1' | translate:{value:assets} }} </li> 2. "Withdrawm ...