python库函数Map, Filter and 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)
map就可以把这个函数简化,如下所示:
items = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, items))
大多数情况下,我们把 lambda结合map一起使用,替代传统的输入,如下所示:
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), funcs))
print(value) # Output:
# [0, 0]
# [1, 2]
# [4, 4]
# [9, 6]
# [16, 8]
二 Filter:创建一个让函数返回为True的链表元素,下面是一个简洁的例子:
number_list = range(-5,5)
less_than_zero = list(filter(lambda x: x < 0, number_list))
print(less_than_zero) number_list = range(-5,5)
less_than_zero = list(filter(lambda x: x != 0, number_list))
print(less_than_zero)
三 reduce:在链表元素的循环计算方面有着广泛的用途,
正常情况下,计算的是这样写的:
product = 1
list = [1, 2, 3, 4]
for num in list:
product = product * num # product = 24
在使用reduce的情况下,是这样计算的:
from functools import reduce
product = reduce((lambda x, y: x * y), [1, 2, 3, 4]) # Output: 24
一下子简洁了很多。
参考文档:
1 http://book.pythontips.com/en/latest/map_filter.html
python库函数Map, Filter and Reduce的用法的更多相关文章
- [译]PYTHON FUNCTIONS - MAP, FILTER, AND REDUCE
map, filter, and reduce Python提供了几个函数,使得能够进行函数式编程.这些函数都拥有方便的特性,他们可以能够很方便的用python编写. 函数式编程都是关于表达式的.我们 ...
- Python之内建函数Map,Filter和Reduce
Python进阶 map,filter, reduce是python常用的built-in function. 且常与lambda表达式一起用. 其中: map 形式:map(function_to_ ...
- Python Map, Filter and Reduce
所属网站分类: python基础 > 函数 作者:慧雅 原文链接: http://www.pythonheidong.com/blog/article/21/ 来源:python黑洞网 www. ...
- 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函数和reduce函数(转)
map函数 map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 例 ...
- js Array 中的 map, filter 和 reduce
原文中部分源码来源于:JS Array.reduce 实现 Array.map 和 Array.filter Array 中的高阶函数 ---- map, filter, reduce map() - ...
- python的高阶函数(map,filter,sorted,reduce)
高阶函数 关注公众号"轻松学编程"了解更多. 1.MapReduce MapReduce主要应用于分布式中. 大数据实际上是在15年下半年开始火起来的. 分布式思想:将一个连续的字 ...
- [Python学习笔记-002] lambda, map, filter and reduce
1. lambda lambda, 即匿名函数,可以理解为跟C语言的宏类似.例如: >>> max = lambda x, y: x if x > y else y >& ...
随机推荐
- week07 13.3 NewsPipeline之 三News Deduper之 tf_idf 查重
我们运行看结果 安装包sklearn 安装numpy 安装scipy 终于可以啦 我们把安装的包都写在文件里面吧 4行4列 轴对称 只需要看一半就可以 横着看 竖着看都行 数值越接近1 表示越相似 我 ...
- Session & Cookie 简介
(一)简介 会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通 ...
- php 获取数组深度的key
1.数组 深度遍历 function fun($a,&$b) { foreach ($a as $k=>$val) { if (is_array($val)) { $b[]=$k; fu ...
- Android 查阅博客2_APT
https://mp.weixin.qq.com/s/3zrAzOUGpovRRbuYnce3uw APT(Annotation Processing Tool) 即注解处理器,是一种注解处理工具,用 ...
- springboot读取配置注解@ConfiguratioinProperties和@Value的区别
- 做u盘启动重装系统 进winPE 出现 cdboot:couldn't find ntldr 解决办法
公司的QA本来用的ubuntu系统 觉得不是很好使 就找我重装win10系统 之前有重装过系统 就信心满满的答应了 我拿出U盘 把U盘格式化了下 去下载了个雨林木风的win10 系统(ISO文件) ...
- python调用webservice接口
使用suds这个第三方模块 from suds.client import Clienturl = 'http://ip:port/?wsdl'cilent=Client(url)print cile ...
- nodejs+https 使用openssl (window)
HTML的getUsermedia必是要安全的连接 比如 localhost.127.0.0.1 .https chrome才让调用摄像头 1.申请域名.备案.域名解析 2.openssl生成 打开g ...
- Log4Net web.config配置
1 .[assembly: log4net.Config.XmlConfigurator(ConfigFile = "web.config", Watch = true)] 写 ...
- Codeforces Round #436 (Div. 2)C. Bus 模拟
C. Bus time limit per test: 2 seconds memory limit per test: 256 megabytes input: standard input out ...