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的更多相关文章

  1. Map,Filter和Reduce

    转自:https://www.aliyun.com/jiaocheng/444967.html?spm=5176.100033.1.13.xms8KG 摘要:Map,Filter和Reduce三个函数 ...

  2. Python Map, Filter and Reduce

    所属网站分类: python基础 > 函数 作者:慧雅 原文链接: http://www.pythonheidong.com/blog/article/21/ 来源:python黑洞网 www. ...

  3. [译]PYTHON FUNCTIONS - MAP, FILTER, AND REDUCE

    map, filter, and reduce Python提供了几个函数,使得能够进行函数式编程.这些函数都拥有方便的特性,他们可以能够很方便的用python编写. 函数式编程都是关于表达式的.我们 ...

  4. Python之内建函数Map,Filter和Reduce

    Python进阶 map,filter, reduce是python常用的built-in function. 且常与lambda表达式一起用. 其中: map 形式:map(function_to_ ...

  5. js Array 中的 map, filter 和 reduce

    原文中部分源码来源于:JS Array.reduce 实现 Array.map 和 Array.filter Array 中的高阶函数 ---- map, filter, reduce map() - ...

  6. python库函数Map, Filter and Reduce的用法

    python中有三个函数式编程极大的简化了程序的复杂性,这里就做一下讨论和记录. 一 Map:应用在链表输入所有元素的函数,它的格式如下所示: map(function_to_apply, list_ ...

  7. [Python学习笔记-002] lambda, map, filter and reduce

    1. lambda lambda, 即匿名函数,可以理解为跟C语言的宏类似.例如: >>> max = lambda x, y: x if x > y else y >& ...

  8. python3的map(),filter()和reduce()函数总结

    这三个都是内置的常用高阶函数(Higher-order function),用法如下: map()函数接收两个参数,一个是函数,一个是Iterable,map将传入的函数依次作用到序列的每个元素,并把 ...

  9. python的高阶函数(map,filter,sorted,reduce)

    高阶函数 关注公众号"轻松学编程"了解更多. 1.MapReduce MapReduce主要应用于分布式中. 大数据实际上是在15年下半年开始火起来的. 分布式思想:将一个连续的字 ...

随机推荐

  1. Ruby中写换行

    Ruby中写换行 print("Hello,\nRuby\n!\n") print("Hello, Ruby ! ") 这两个竟然是一样的:就是说,可以直接回车 ...

  2. [Tools] Create your own mobile emulator device by using Chrome dev tool

    Using the New Device Emulation Interface The Device Emulation interface changed a bit with the newer ...

  3. 【Allwinner ClassA20类库分析】 2.free pascal语法及结构简析

        上一节介绍了Lazarus一般的开发操作流程,对于不熟悉pascal语言的朋友可能看的还是不大明确.不知道pascal代码里都应该包括什么或起什么作用.这回就简单地介绍下语法及代码文件的结构. ...

  4. 第二次phython作业

    第一题:编写程序,生成一个包含50个随机整数的列表,然后删除其中所有奇数.(注意保证删除操作的效率) import random x=[random.randint(0,100)for i in ra ...

  5. fieldset 标签 -- 对表单进行分组

    转自:https://xhmaomy-163-com.iteye.com/blog/1066977 fieldset——一个不常用的HTML标签 fieldset 标签 -- 对表单进行分组 在for ...

  6. 你不知道的JavaScript(八)逻辑运算

    JS的逻辑运算结果和其他一些强类型语言差别比较大,也比较容易让人产生困惑,看下面的例子: <script type="text/javascript"> var a = ...

  7. POJ 1182 食物链 (并查集解法)(详细注释)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 78510   Accepted: 23396 Description ...

  8. 【原创】Apache集群报Service Temporarily Unavailable的解决

    Apache的集群突然时不时的报出以下错误: Service Temporarily Unavailable The server is temporarily unable to service y ...

  9. Eclipse安装Web插件

    方法/步骤     本次安装教程,我把所有的步骤都写在了图片中,大家仔细查看图片即可,希望能帮到大家   1.选择菜单栏上的“Help”   选择Install New Software   在弹出的 ...

  10. nginx的location 匹配的规则问题

    正则解释: ~ #匹配一个正则匹配,区分大小写~* #匹配一个正则,不区分大小写^~ #普通字符匹配,如果该选择匹配不匹配别的选项,一般用来匹配目录= #精确匹配 匹配案例:location = / ...