高阶函数map(),filter(),reduce()
接受函数作为参数,或者把函数作为结果返回的函数是高阶函数,官方叫做 Higher-order functions。
map()和filter()是内置函数。在python3中,reduce()已不再是内置函数,被放到了functools模块里面,这个函数最常用于求和。
另外,列表推导式和生成器表达式具有map()和filter()两个函数的功能,而且更易于阅读。
map()
在python3中,map()函数返回的是一个可迭代的map对象,可用list()函数转换为列表。
map()函数将参数序列中的元素传递给参数函数,然后将生成的结果返回组成新的可迭代对象。
map()函数可传递多个参数序列,运行方式与zip()函数类似,这里不再细说。
>>> help(map)
Help on class map in module builtins:
class map(object)
| map(func, *iterables) --> map object
|
| Make an iterator that computes the function using arguments from each of the iterables. Stops when the shortest iterable is exhausted.
Return an iterator that applies function to every item of iterable, yielding the results.
If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted.
>>> map(lambda x: x ** 2, [1, 2, 3, 4, 5])
<map object at 0x0000000002E86208>
>>> list(map(lambda x: x ** 2, [1, 2, 3, 4, 5]))
[1, 4, 9, 16, 25]
>>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])
<map object at 0x0000000002E86208>
>>> list(map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10]))
[3, 7, 11, 15, 19]
filter()
在python3中,filter()函数返回的是一个可迭代的filter对象,同样可用list()函数转换为列表。
filter()函数用来过滤序列,参数函数为判断函数,参数序列中判断为真的元素返回组成新的可迭代对象。
>>> help(filter)
Help on class filter in module builtins:
class filter(object)
| filter(function or None, iterable) --> filter object
|
| Return an iterator yielding those items of iterable for which function(item) is true. If function is None, return the items that are true.
Construct an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.
Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not None and (item for item in iterable if item) if function is None.
>>> filter(lambda x: x % 3, range(20))
<filter object at 0x0000000002B7DBE0>
>>> list(filter(lambda x: x % 3, range(20)))
[1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19]
reduce()
reduce()函数会对参数序列中的元素从左到右进行累积,最终reduce为单个value。
reduce()函数直接返回value,而不是迭代器。
>>> from functools import reduce
>>> help(reduce)
Help on built-in function reduce in module _functools:
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.
>>> reduce(lambda x,y: x+y, [1,2,3,4,5])
15
reduce()函数的参数函数必须跟两个参数,完成从左到右累积。如果不是两个参数,会报 TypeError 异常。
>>> reduce(lambda x: x + 1, [1,2,3,4,5])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: <lambda>() takes 1 positional argument but 2 were given
参考:
https://docs.python.org/3/library/functools.html
https://docs.python.org/3/library/functions.html#map
https://docs.python.org/3/library/functions.html#filter
https://docs.python.org/3/library/functools.html#functools.reduce
高阶函数map(),filter(),reduce()的更多相关文章
- 高阶函数-map/filter/reduce
什么样的函数叫高阶函数: 条件:1.函数接受函数作为参数 2.函数的返回值中包含函数 高阶函数之----map函数 map(func, *iterables) --> map objectnum ...
- 高阶函数map,filter,reduce的用法
1.filter filter函数的主要用途是对数组元素进行过滤,并返回一个符合条件的元素的数组 let nums = [10,20,30,111,222,333] 选出nums中小于100的数: l ...
- Python高阶函数map、reduce、filter、sorted的应用
#-*- coding:utf-8 -*- from selenium import webdriver from selenium.webdriver.support.wait import Web ...
- python的高阶函数(map,filter,sorted,reduce)
高阶函数 关注公众号"轻松学编程"了解更多. 1.MapReduce MapReduce主要应用于分布式中. 大数据实际上是在15年下半年开始火起来的. 分布式思想:将一个连续的字 ...
- 数组的高阶方法map filter reduce的使用
数组中常用的高阶方法: foreach map filter reduce some every 在这些方法中都是对数组中每一个元素进行遍历操作,只有foreach是没有 ...
- js高阶函数map和reduce
map 举例说明,比如我们有一个函数f(x)=x2,要把这个函数作用在一个数组[1, 2, 3, 4, 5, 6, 7, 8, 9]上,就可以用map实现如下: 由于map()方法定义在JavaScr ...
- python之高阶函数--map()和reduce()
以下为学习笔记:来自廖雪峰的官方网站 1.高阶函数:简单来说是一个函数里面嵌入另一个函数 2.python内建的了map()和reduce()函数 map()函数接收两参数,一个是函数,一个是Iter ...
- python学习笔记1 -- 函数式编程之高阶函数 map 和reduce
我用我自己,就是高阶函数,直接表现就是函数可以作为另一个函数的参数,也可以作为返回值 首先一个知识点是 函数的表现形式,印象中的是def fw(参数)这种方式定义一个函数 python有很多的内置函 ...
- JS高阶函数--------map、reduce、filter
一.filter filter用于对数组进行过滤.它创建一个新数组,新数组中的元素是通过检查指定数组中符合条件的所有元素. 注意: filter() 不会对空数组进行检测. 注意: filter() ...
随机推荐
- jQuery.bsgrid
http://thebestofyouth.com/bsgrid/ 支持json.xml数据格式,皮肤丰富并且容易定制,支持表格编辑.本地数据.导出参数构建等实用便捷的功能,容易扩展,更拥有丰富的示例 ...
- RESTful API设计概要
一.简介 1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Roy Fiel ...
- 2017-2018-2 20155203《网络对抗技术》 Exp7:网络欺诈防范
1.基础问题回答 (1)通常在什么场景下容易受到DNS spoof攻击 连接无线网络,和恶意攻击者处在同一局域网下. (2)在日常生活工作中如何防范以上两攻击方法 首先决不去点击浏览器都认为不安全的网 ...
- Oracle出现与并行相关的ORA-00600时的调查方法
出现了 ORA-00600[kxfpqsod_qc_sod], 如何调查呢? 例如:从trace 文件的 Call Stack,可以看到 Error: ORA-600 [kxfpqsod_qc_sod ...
- [清华集训2015 Day2]矩阵变换-[稳定婚姻模型]
Description 给出一个N行M列的矩阵,保证满足以下性质: M>N. 矩阵中每个数都是 [0,N]中的自然数. 每行中, [1,N]中每个自然数刚好出现一次,其余的都是0. 每列中,[1 ...
- Spring MVC统一异常处理
实际上Spring MVC处理异常有3种方式: (1)一种是在Controller类内部使用@ExceptionHandler使用注解实现异常处理: 可以在Controller内部实现更个性化点异常处 ...
- 详解C#7.0新特性
1. out 变量(out variables) 以前我们使用out变量必须在使用前进行声明,C# 7.0 给我们提供了一种更简洁的语法 “使用时进行内联声明” .如下所示: 1 var input ...
- Jmeter(二十二)_脚本上传Gitlab
Docker部署接口自动化持续集成环境第四步,代码上传到远程仓库! 接上文:Ubuntu部署jmeter与ant Gitlab在容器中部署好了之后,本地直接打开.我们可以在里面创建项目,上传脚本. 新 ...
- npm install的几种命令形式区别
转自未来与传说.jigetage 我们在使用 npm install 安装模块的时候 ,一般会使用下面这几种命令形式: npm install moduleName # 安装模块到项目目录下 npm ...
- 批量备份H3C交换机路由器配置
第一种(使用ftp下载配置文件): #!/bin/bash datetime=`date +%Y%m%d` BAKTIME=`date +%Y%m%d%H%M%S` user="admin& ...