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 >& ...
随机推荐
- css选择器querySelectorAll
* querySelectorAll(css的选择器)* 通过css的选择器获取到的一组元素* 获取的也是类数组** 主语* document 从整个页面去获取一组元素* 父级 从父级下去获取一组元素 ...
- 页面JS实现按钮点击增加输入框
学习记录 https://www.tuicool.com/articles/byUf2qe
- 将python、pip 加入环境变量
加python: CMD里输: path=%path%;C:\Python27 其中 C:\Python27 为python的exe所在的文件夹 加pip: CMD里输: path= ...
- CSS3 white-space属性
white-space 属性设置如何处理元素内的空白. 可能的值 值 描述 normal 默认.空白会被浏览器忽略. pre 空白会被浏览器保留.其行为方式类似 HTML 中的 <pre> ...
- 在Laravel中使用mongoDB
https://blog.csdn.net/weixin_38682852/article/details/80840678?utm_source=blogxgwz1 https://blog.csd ...
- mysql linux安装
Mysql(使用版本5.7.25) 1. 检查是否已安装 #rpm -qa|grep -i mysql 2. 下载安装包 网址:https://dev.mysql.com/downloads/my ...
- [leetcode]26. Remove Duplicates from Sorted Array有序数组去重(单个元素只出现一次)
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- git创建新的分支
1.本地创建一个新的分支 git branch develop 2.切换到新创建的分支 git checkout develop 3.将新的分支发布到gitlab上 git push origin d ...
- PHP开发——数组
数组的概念 l 数组是一组数的集合.如:$arr = array(1,2,3,4,5,6) l 标量数据类型是一个值的容器,而数组就是多个值的容器. 数组的分类 l 枚举数组:数组元素的下标(索 ...
- Elasticsearch tshark 封包分析 (转)
Elasticsearch tshark 封包分析 使用wireshark能解決許多網路問題,將側錄下來的封包傳至Elasticsearch上方便分析製作及時報表.tshark為wireshark的命 ...