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
最新文章
- 【干货分享】流程DEMO-制度发文和干部任免
- Asp.net 面向接口可扩展框架之使用“类型转化基础服务”测试四种Mapper(AutoMapper、EmitMapper、NLiteMapper及TinyMapper)
- Bounce.js – 快速创建漂亮的 CSS3 动画效果
- 编译安装chkrootkit出现的问题
- HDU 5862 Counting Intersections (树状数组)
- tomcat的 JNDI 配置
- WPF编程学习——动画
- 【Android UI设计与开发】之具体解释ActionBar的使用
- 转载 50种方法优化SQL Server数据库查询
- zoj 2067 White Rectangles
- 图解SSIS监视文件夹并自动导入数据
- SQLServer中的执行计划缓存由于长时间缓存对性能造成的干扰
- MySQL Percona server 5.5 安装审计插件
- Oracle通过Navicat导入表数据与机构,数据无法直接查询,需要加双引号的问题
- 使用sphinx制作接口文档并托管到readthedocs
- linux安装命令出错(could not resolve host mirrorlist.centos.org)
- 自由拖拽DIV实现
- ZZ:git只clone仓库中指定子目录和指定文件的实现
- C语言表驱动法编程实践
- error: style attribute '@android:attr/windowEnterAnimation' not found.
热门文章
- 上传文件 nginx 413错误
- iOS开发 之 不要告诉我你真的懂isEqual与hash!
- hdoj--1010--Tempter of the Bone(搜索+奇偶剪枝)
- c:\Windows\System32\drivers\etc\hosts的作用
- BZOJ 3175 最大独立集
- 【算法】第二类斯特林数Stirling
- iOS系统的特点-iOS为什么运行更流畅
- swift语言点评十九-类型转化与检查
- swift语言点评十八-异常与错误
- C# 将string 转换为二维码图片,然后转为base64字符串编码 。