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 time through the loop, x gets one element from the list. the += operator provides a short way to update a variable:
Total += x is equivalent to: total = total + x
As the loop executes, total accumulates the sum of the elements; a variable used this way is sometimes called an accumulator. Adding up the elements of a list is such a common operation that Python provides it as a built-in function, sum:
An operation like this that combines a sequence of elements into a single value is sometimes called reduce. Sometimes you want to traverse one list while building another. For example, the following function takes a list of strings and returns a new list that contains capitalized strings:
res is initialized with an empty list; each time through the loop, we append the next element. So res is another kind of accumulator. An operation like capitalize_all is sometimes called a map because it ‘maps’ a function (in this case the method capitalize) onto each of the elements in a sequence.
Another common operation is to select some of the elements from a list and return a sublist. For example, the following function takes a list of strings and returns a list that contain only the uppercase strings:
isupper is a string method that returns True if the string contains only upper case letters. An operation like only_upper is called a filter because it selects some of the elements and filters out the others.
Most common list operations can be expressed as a combination of map, filter and reduce. Because these operations are so common, Python provides language features to support them, including the built-in function reduce and an operator called a ‘list comprehension’. But these features are idiomatic to Python.
Another simple way:
list comprehension
A compact way to process all or part of the elements in a sequence and return a list with the results. result = ['{:#04x}'.format(x) for x in range(256) if x % 2 == 0] generates a list of strings containing even hex numbers (0x..) in the range from 0 to 255. The if clause is optional. If omitted, all elements in range(256) are processed.
from Thinking in Python
Map, filter and reduce的更多相关文章
- Map,Filter和Reduce
转自:https://www.aliyun.com/jiaocheng/444967.html?spm=5176.100033.1.13.xms8KG 摘要:Map,Filter和Reduce三个函数 ...
- 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编写. 函数式编程都是关于表达式的.我们 ...
- Python之内建函数Map,Filter和Reduce
Python进阶 map,filter, reduce是python常用的built-in function. 且常与lambda表达式一起用. 其中: map 形式:map(function_to_ ...
- js Array 中的 map, filter 和 reduce
原文中部分源码来源于:JS Array.reduce 实现 Array.map 和 Array.filter Array 中的高阶函数 ---- map, filter, reduce map() - ...
- python库函数Map, Filter and Reduce的用法
python中有三个函数式编程极大的简化了程序的复杂性,这里就做一下讨论和记录. 一 Map:应用在链表输入所有元素的函数,它的格式如下所示: map(function_to_apply, list_ ...
- [Python学习笔记-002] lambda, map, filter and reduce
1. lambda lambda, 即匿名函数,可以理解为跟C语言的宏类似.例如: >>> max = lambda x, y: x if x > y else y >& ...
- python3的map(),filter()和reduce()函数总结
这三个都是内置的常用高阶函数(Higher-order function),用法如下: map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把 ...
- python的高阶函数(map,filter,sorted,reduce)
高阶函数 关注公众号"轻松学编程"了解更多. 1.MapReduce MapReduce主要应用于分布式中. 大数据实际上是在15年下半年开始火起来的. 分布式思想:将一个连续的字 ...
随机推荐
- intellij idea 打开两个 terminal
intellij idea 打开两个 terminal alt+f12可以打开terminal,在terminal窗口左侧点击绿色的加号,就可以又打开一个terminal,用tab标签展示:
- Java5新特性之枚举
1. 概念 首先,枚举并非一种新技术,而是一种基础数据类型.它隶属于两种基础类型中的值类型,例如以下: 2. 为什么要有枚举 枚举在真正的开发中是非经常常使用的,它的作用非常easy也非常纯粹:它 ...
- mysql int(m)与int(m)的差别
预计大多数開始接触mysql的朋友们都会有这个问题:int(M) 里面的数值究竟是什么意思? 依据相关资料总结了下: int(M) zerofill,加上zerofill后M才表现出有点点效果,比方 ...
- 【HDOJ 5407】 CRB and Candies (大犇推导
pid=5407">[HDOJ 5407] CRB and Candies 赛后看这题题解仅仅有满眼的迷茫------ g(N) = LCM(C(N,0),C(N,1),...,C(N ...
- iOS开发 之 不要告诉我你真的懂isEqual与hash!
目录 为什么要有isEqual方法? 如何重写自己的isEqual方法? 为什么要有hash方法? hash方法什么时候被调用? hash方法与判等的关系? 如何重写自己的hash方法? 为什么要有i ...
- [雅礼NOIP2018集训] day6
打满暴力好像是一种挑战,已经连续几天考试最后一个小时自闭了,因为自以为打完了暴力,然而,结果往往差强人意 大概是考试的策略有些问题 T1: 我们设$g[x]$为在x时取小于等于m个物品的最大价值,下面 ...
- Mysql实战45讲 06讲全局锁和表锁:给表加个字段怎么有这么多阻碍 极客时间 读书笔记
Mysql实战45讲 极客时间 读书笔记 Mysql实战45讲 极客时间 读书笔记 笔记体会: 根据加锁范围:MySQL里面的锁可以分为:全局锁.表级锁.行级锁 一.全局锁:对整个数据库实例加锁.My ...
- 访问Storm ui界面,出现org.apache.storm.utils.NimbusLeaderNotFoundException: Could not find leader nimbus from seed hosts ["master"]. Did you specify a valid list of nimbus hosts for confi的问题解决(图文详解)
不多说,直接上干货! 前期博客 apache-storm-0.9.6.tar.gz的集群搭建(3节点)(图文详解) apache-storm-1.0.2.tar.gz的集群搭建(3节点)(图文详解)( ...
- NPInter数据集的奇葩标号的出坑秘籍
这篇恐怕是有始以来命名最无奈标题了.需要写一下攻略. 业内人士都熟知NPInter,但是该数据库一直以来访问受限.不过终于能访问得到数据集. 但是蛋疼的是2.0的数据库id的命名方法实在奇葩,想了很多 ...
- 读 Real-Time Rendering 收获 - chapter 4. transform
chapter 4. Transform p54 affine transform p57 all rotation matrices have a determinant of one and ar ...