Map、Filter和Reduce函数(Python)
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)的更多相关文章
- python3的map(),filter()和reduce()函数总结
这三个都是内置的常用高阶函数(Higher-order function),用法如下: map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把 ...
- Python Map, Filter and Reduce
所属网站分类: python基础 > 函数 作者:慧雅 原文链接: http://www.pythonheidong.com/blog/article/21/ 来源:python黑洞网 www. ...
- [译]PYTHON FUNCTIONS - MAP, FILTER, AND REDUCE
map, filter, and reduce Python提供了几个函数,使得能够进行函数式编程.这些函数都拥有方便的特性,他们可以能够很方便的用python编写. 函数式编程都是关于表达式的.我们 ...
- Map,Filter和Reduce
转自:https://www.aliyun.com/jiaocheng/444967.html?spm=5176.100033.1.13.xms8KG 摘要:Map,Filter和Reduce三个函数 ...
- 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 ...
- Python的map、filter、reduce函数 [转]
1. map函数func作用于给定序列的每个元素,并用一个列表来提供返回值. map函数python实现代码: def map(func,seq): mapped_seq = [] fo ...
- python迭代和解析(3):range、map、zip、filter和reduce函数
解析.迭代和生成系列文章:https://www.cnblogs.com/f-ck-need-u/p/9832640.html range range()是一个内置函数,它返回一个数字序列,功能和Li ...
- python的高阶函数(map,filter,sorted,reduce)
高阶函数 关注公众号"轻松学编程"了解更多. 1.MapReduce MapReduce主要应用于分布式中. 大数据实际上是在15年下半年开始火起来的. 分布式思想:将一个连续的字 ...
- python中的map、filter、reduce函数
三个函数比较类似,都是应用于序列的内置函数.常见的序列包括list.tuple.str. 1.map函数 map函数会根据提供的函数对指定序列做映射. map函数的定义: map(function ...
- python_08 函数式编程、高阶函数、map、filter、reduce函数、内置函数
函数式编程 编程方法论: 1.面向过程 找到解决问题的入口,按照一个固定的流程去模拟解决问题的流程 (1).搜索目标,用户输入(配偶要求),按照要求到数据结构内检索合适的任务 (2)表白,表白成功进入 ...
随机推荐
- VueJS基础框架代码介绍
参考文档 https://vuejs.bootcss.com/v2/api/ https://router.vuejs.org/zh-cn/essentials/getting-started.htm ...
- 偏函数(partial)
from functools import partial def add(a,b,c,d): return a+b+c+d add = partial(add,1,2) print(add(3,4) ...
- MySQL执行计划之EXPLAIN基本解释说明
一.EXPLAIN使用潜规则 explain + sql语句 例如: EXPLAIN SELECT * FROM `t_user`; 二. 表头字段详解 (1) id-----> 表的读取顺序 ...
- SpringMVC的 ModelAndView
使用ModelAndView类用来存储处理完后的结果数据,以及显示该数据的视图. public class ModelAndView { /** View instance or view name ...
- (转)教你分分钟搞定Docker私有仓库Registry
转:https://www.cnblogs.com/Javame/p/7389093.html 一.什么是Docker私有仓库Registry 官方的Docker hub是一个用于管理公共镜像的好地方 ...
- 命令连接redis
cd到redis的bin目录 ./redis-cli -h 输入info,看是否要验证 清空所有key FLUSHALL
- zabbix4.0部署
1.环境检查 uname -r getenforce systemctl status firewalld.service 2.设置解析,自建yum源(可选) /etc/hosts #!/bin/ba ...
- OpenCV2.4.8 + CUDA7.5 + VS2013 配置
配置过程主要参考:https://initialneil.wordpress.com/2014/09/25/opencv-2-4-9-cuda-6-5-visual-studio-2013/ 1.为什 ...
- ! Failed at the chromedriver@2.35.0 install script.
npm install 过程中报错 解决方法 运行 npm install chromedriver --chromedriver_cdnurl=http://cdn.npm.taobao.org/d ...
- std::sort的详细用法
#include <algorithm> #include <functional> #include <array> #include <iostream& ...