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 >& ...
随机推荐
- git pull时解决分支分叉(branch diverged)问题
git pull时出现分支冲突(branch diverged) $ git status # On branch feature/worker-interface # Your branch and ...
- hdu5698瞬间移动-(杨辉三角+组合数+乘法逆元)
瞬间移动 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submis ...
- javaScript+html5实现图片拖拽
源码: <!DOCTYPE html><html><head> <meta charset="utf-8"/> <title& ...
- pyspider--post
#!/usr/bin/env python# -*- encoding: utf-8 -*-# Created on 2018-08-19 14:47:28# Project: HBGGZY_SBJ ...
- etcd集群部署与遇到的坑
在k8s集群中使用了etcd作为数据中心,在实际操作中遇到了一些坑.今天记录一下,为了以后更好操作. ETCD参数说明 —data-dir 指定节点的数据存储目录,这些数据包括节点ID,集群ID,集群 ...
- POJ-2533.Longest Ordered Subsequence (LIS模版题)
本题大意:和LIS一样 本题思路:用dp[ i ]保存前 i 个数中的最长递增序列的长度,则可以得出状态转移方程dp[ i ] = max(dp[ j ] + 1)(j < i) 参考代码: # ...
- C#使用 params object[] 将参数个数不一样的方法 集成一个
getChange("1"); getChange("1","2"); public string getChange(params obj ...
- 10.13 新版本go on~
上午1.5 终审 and 排期 合同管理那边又是切换选项时各种联动,我第一想法是 好麻烦,不想做这个...第二想法才是给我做吧 锻炼锻炼我 然后 分任务的时候 分给我了,,哈哈 开心 虽然我没想躲 但 ...
- 113. Path Sum II 输出每个具体路径
[抄题]: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the gi ...
- Sass入门及知识点整理
Sass 快速入门 | SASS 中文网 文档链接:https://www.sasscss.com/getting-started/ 前言 之前整理了一篇关于Less的,现在就来整理一下关于Sass的 ...