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

时间: 2024-10-11 16:35:32

Map, filter and reduce的相关文章

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

map, filter, and reduce Python提供了几个函数,使得能够进行函数式编程.这些函数都拥有方便的特性,他们可以能够很方便的用python编写. 函数式编程都是关于表达式的.我们可以说,函数式编程是一种面向表达式的编程. Python提供的面向表达式的函数有: map(aFunction, aSequence) filter(aFunction, aSequence) reduce(aFunction, aSequence) lambda list comprehensio

python map、filter、reduce

Python has a few functions that are useful for this sort of “functional programming”: map, filter, and reduce. 4 (In Python 3.0, these are moved to the functools module.) The map and filter functions are not really all that useful in current versions

C#数组的Map、Filter、Reduce等价方法

在Javascript.Python等语言里,Map.Filter和Reduce是数组的常用方法,可以让你在实现一些数组操作时告别循环,具有很高的实用价值.它们三个的意义大家应该都清楚,有一个十分形象的解释如下: 然而,支持lambda表达式的C#也有类似的方法,但不是这样命名的.实现IEnumerable接口的类(如List.HashSet.继承Array的类等)都有如下等价方法: “Map” => Select方法 “Filter” => Where方法 “Reduce” => Ag

Python学习:映射函数(map)和函数式编程工具(filter和reduce)

在序列中映射函数map map函数会对一个序列对象中的每一个元素应用被传入的函数,并且返回一个包含了所有函数调用结果的一个列表. 例1: def sum(x):     return x + 10 L1 = [1,2,3,4,5,6,7] L = map(sum, L1) #结果为[11, 12, 13, 14, 15, 16, 17] map还有更高级的使用方法,例如提供了序列作为参数,它能够并行返回分别以每个序列中的元素作为函数对应参数得到的结果的列表.如例2所示. 例2: def sum(

使用python实现内置map,filter,reduce函数

map函数 # -*- coding: cp936 -*- def myselfmap(f,*args):     def urgymap(f,args):         if args==[]:             return []         else:             return [f(args[0])]+urgymap(f,args[1:])     if list(args)[0]==[]:             #*args有多个参数被传递时返回tuple  

Swift高阶函数:Map,Filter,Reduce

闭包介绍 Swift一大特性便是使用简洁的头等函数/闭包语法代替了复杂的blocks语法.希望我们在Swift中不再需要像fuckingblocksyntax中所描述的语法.(译者注:头等函数-即可将函数当作参数传递给其他的函数,或从其他的函数里返回出值,并且可以将他们设定为变量,或者将他们存储在数据结构中) 闭包是自包含的blocks,它能在代码中传递和使用. 本文我们将重点介绍匿名定义的闭包(如:定义成内联的且不具名)也称匿名闭包.我们能够将其作为参数传递给其他函数/方法或者将其作为返回值.

Swift详解之五-----------map,filter,reduce

map,filter,reduce 注:本文为作者自己总结,过于基础的就不再赘述 ,都是亲自测试的结果.如有错误或者遗漏的地方,欢迎指正,一起学习. 关于Swift 中String .数组 .字典的基本用法这里就不再赘述了,这些都很简单 不会的 在用得时候baidu下就行了.这里主要看下这几个高阶函数 map map方法,其获取一个闭包表达式作为其唯一参数. 数组中的每一个元素调用一次该闭包函数,并返回该元素所映射的值(也可以是不同类型的值). 具体的映射方式和返回值类型由闭包来指定. 当提供给

Swift函数编程之Map、Filter、Reduce

在Swift语言中使用Map.Filter.Reduce对Array.Dictionary等集合类型(collection type)进行操作可能对一部分人来说还不是那么的习惯.对于没有接触过函数式编程的开发者来说,对集合类型中的数据进行处理的时候第一反应可能就是采用for in遍历.本文将介绍一些Swift中可以采用的新方法. Map Map函数会遍历集合类型并对其中的每一个元素进行同一种的操作.Map的返回值是一个所得结果的数组.例如:我们要对一个数组里面的数据进行平方操作,常见的代码如下:

Python中map,filter,reduce的应用

事例1: l=[('main', 'router_115.236.xx.xx', [{'abc': 1}, {'dfg': 1}]), ('main', 'router_183.61.xx.xx', [{'abc': 0}, {'dfg': 1}]), ('main', 'router_52.11.xx.xx', [{'abc': 0}, {'dfg': 1}]), ('main', 'router_183.17.xx.xx', [{'abc': 1}, {'dfg': 1}]) ] 检查参数l