Map

map(function_to_apply, list_of_inputs)

设有以下代码:

>>> items = [1, 2, 3, 4, 5]
>>> squared = []
>>> for i in items:
... squared.append(i**2)
...
>>> squared
[1, 4, 9, 16, 25]

这段代码实现的功能用map函数可以两行完成:

items = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, items))

高级一点地,可以用一列函数作为输入:

def multiply(x):
return (x*x)
def add(x):
return (x+x) funcs = [multiply, add]
for i in range(5):
value = list(map(lambda x: x(i)+1, funcs))
print(value) >>>
[1, 1]
[2, 3]
[5, 5]
[10, 7]
[17, 9]

Filter

Filter creates a list of elements for which a function returns true.

>>> number_list = range(-5,5)
>>> less_than_zero = list(filter(lambda x: x<0, number_list))
>>> print(less_than_zero)
[-5, -4, -3, -2, -1]

经过_filter_函数_过滤_,条件判断为真的结果的存入list。另外值得注意的地,_filter_是_built-in_函数,运行速度更快。

Reduce

Reduce is a really useful function for performing some computation on a list and returning the result. It applies a rolling computation to sequential pairs of values in a list.

>>> from functools import reduce
>>> product = reduce((lambda x, y: x * y), [1, 2, 3, 4])
>>> product
24

Map、Filter和Reduce函数(Python)的更多相关文章

  1. python3的map(),filter()和reduce()函数总结

    这三个都是内置的常用高阶函数(Higher-order function),用法如下: map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把 ...

  2. Python Map, Filter and Reduce

    所属网站分类: python基础 > 函数 作者:慧雅 原文链接: http://www.pythonheidong.com/blog/article/21/ 来源:python黑洞网 www. ...

  3. [译]PYTHON FUNCTIONS - MAP, FILTER, AND REDUCE

    map, filter, and reduce Python提供了几个函数,使得能够进行函数式编程.这些函数都拥有方便的特性,他们可以能够很方便的用python编写. 函数式编程都是关于表达式的.我们 ...

  4. Map,Filter和Reduce

    转自:https://www.aliyun.com/jiaocheng/444967.html?spm=5176.100033.1.13.xms8KG 摘要:Map,Filter和Reduce三个函数 ...

  5. Map, filter and reduce

    To add up all the numbers in a list, you can use a loop like this: Total is initialized to 0. Each t ...

  6. Python的map、filter、reduce函数 [转]

    1. map函数func作用于给定序列的每个元素,并用一个列表来提供返回值. map函数python实现代码: def map(func,seq): mapped_seq = []        fo ...

  7. python迭代和解析(3):range、map、zip、filter和reduce函数

    解析.迭代和生成系列文章:https://www.cnblogs.com/f-ck-need-u/p/9832640.html range range()是一个内置函数,它返回一个数字序列,功能和Li ...

  8. python的高阶函数(map,filter,sorted,reduce)

    高阶函数 关注公众号"轻松学编程"了解更多. 1.MapReduce MapReduce主要应用于分布式中. 大数据实际上是在15年下半年开始火起来的. 分布式思想:将一个连续的字 ...

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

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

  10. python_08 函数式编程、高阶函数、map、filter、reduce函数、内置函数

    函数式编程 编程方法论: 1.面向过程 找到解决问题的入口,按照一个固定的流程去模拟解决问题的流程 (1).搜索目标,用户输入(配偶要求),按照要求到数据结构内检索合适的任务 (2)表白,表白成功进入 ...

随机推荐

  1. I/O性能优化

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11525014.html Linux 系统的 I/O 栈图 I/O性能指标 根据指标找工具 根据工具查指 ...

  2. Delphi Win API 函数 MulDiv

    Delphi Win API 函数 MulDiv 原型:function MulDiv(nNumber, nNumerator, nDenominator: Integer): Integer; st ...

  3. 测试单点登录xml配置

    <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://w ...

  4. hdu1166:敌兵布阵(树状数组或线段树)

    题目描述: 一堆废话不用看...... 输入: 第一行一个整数T,表示有T组数据.每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表 ...

  5. Linux 学习 (五) DNS配置

    没有配置DNS会引起的问题 yum命令 ssh命令等不能进行 错误: Could not resolve host: centos.ustc.edu.cn; 本文例子: CentOS7 下DNS配置 ...

  6. mongodb在linux 上要注意的一些东西

    没有配成开机启动服务,在bin目录下还要使用./mongod去启动,暂时先这样,另外要说的是, child process failed, exited with error number 1说明配置 ...

  7. JS-for..of

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...of 刚刚上网上看到<V8 ...

  8. jmeter添加自定义扩展函数之if判断

    1,打开eclipse,新建maven工程,在pom中引用jmeter核心jar包,具体请看---https://www.cnblogs.com/guanyf/p/10863033.html---,这 ...

  9. Unzip 解压报错

      $ jar xvf pcre-8.10.zip   如果出现 jar:Command not found 要用yum下载 $ yum -y install java-1.6.0-openjdk-d ...

  10. fpm rpm制作

    使用fpm命令制作rpm包并安装 工作中有如下情况需要将文件打包rpm: 避免重复工作,将源码程序打包为rpm 使用yum发布项目,项目打包为rpm 将自己写好的程序打包为rpm,提供给用户下载 其他 ...