Functional Programming Tools
There are three built-in functions that are very useful when used with lists: filter(), map(), and reduce().
filter(function, sequence) returns a sequence consisting of those items from the sequence for which function(item) is true. If sequence is a string or tuple, the result will be of the same type; otherwise, it is always a list. For example, to compute some primes:
>>> def f(x): return x % 2 != 0 and x % 3 != 0
...
>>> filter(f, range(2, 25))
[5, 7, 11, 13, 17, 19, 23]
map(function, sequence) calls function(item) for each of the sequence’s items and returns a list of the return values. For example, to compute some cubes:
>>> def cube(x): return x*x*x
...
>>> map(cube, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
More than one sequence may be passed; the function must then have as many arguments as there are sequences and is called with the corresponding item from each sequence (or None if some sequence is shorter than another). For example:
>>> seq = range(8)
>>> def add(x, y): return x+y
...
>>> map(add, seq, seq)
[0, 2, 4, 6, 8, 10, 12, 14]
reduce(function, sequence) returns a single value constructed by calling the binary function function on the first two items of the sequence, then on the result and the next item, and so on. For example, to compute the sum of the numbers 1 through 10:
>>> def add(x,y): return x+y
...
>>> reduce(add, range(1, 11))
55
If there’s only one item in the sequence, its value is returned; if the sequence is empty, an exception is raised.
A third argument can be passed to indicate the starting value. In this case the starting value is returned for an empty sequence, and the function is first applied to the starting value and the first sequence item, then to the result and the next item, and so on. For example,
>>> def sum(seq):
... def add(x,y): return x+y
... return reduce(add, seq, 0)
...
>>> sum(range(1, 11))
55
>>> sum([])
0
Don’t use this example’s definition of sum(): since summing numbers is such a common need, a built-in function sum(sequence) is already provided, and works exactly like this.
New in version 2.3.
分享到:
相关推荐
Python内置了一些非常有趣、有用的函数,如:filter、map、reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是Python列表方法的三架马车。 1. filter函数的功能相当于...
主要介绍了Python lambda表达式filter、map、reduce函数用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
这篇文章主要介绍了简单了解python filter、map、reduce的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 python中有一些非常有趣的函数,面试的时候可能...
主要介绍了Python中的高级函数map/reduce使用实例,Python内建了map()和reduce()函数,本文就讲解如何使用它,需要的朋友可以参考下
python库,解压后可用。 资源全名:wafer_map-1.1.1-py2-none-any.whl
map() 会根据提供的函数对指定序列做映射。下面通过本文给大家介绍Python map函数及Python map()函数的用法,需要的朋友参考下吧
python 高阶函数:map reduce filter map函数:遍历序列,对序列中每个元素进行操作,最终获取新的序列。 reduce函数:对于序列内所有元素进行累计操作。 filter函数:对于序列中的元素进行筛选,最终获取符合条件的...
这篇文章主要介绍了Pythont特殊语法filter,map,reduce,apply使用方法,需要的朋友可以参考下 (1)lambda lambda是Python中一个很有用的语法,它允许你快速定义单行最小函数。类似于C语言中的宏,可以用在任何需要...
Python之map和reduce共5页.pdf.zip
详解python中三种高阶函数(map,reduce,filter)
文章目录sorted函数一、sort方法二、sorted内置函数三、情景引入filter类一、简单使用二、练习map类语法:一、简单使用二、练习reduce函数语法:一、简单使用二、设置初始值 Python中使用函数作为参数的内置函数和类...
pythonfilter⽤法,python基础教程之Filter使⽤⽅法 python Filter Python中的内置函数filter()主要⽤于过滤序列。 和map类似,filter()也接收⼀个函数和序列,和map()不同的是,filter()把传⼊的函数依次作⽤于每个...
资源分类:Python库 所属语言:Python 资源全名:plot_map-0.3.7.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
reduce()函数也是Python内置的一个高阶函数。reduce()函数接收的参数和 map()类似,一个函数 f,一个list,但行为和 map()不同,今天我们就来详细探讨下
所以,map()作为高阶函数,事实上它把运算规则抽象了,因此,我们不但可以计算简单的f(x)=x2,还可以计算任意复杂的函数,比如,把这个list所有数字转为字
使用pyton对OpenStreetMap地图数据进行清洗 import lxml.etree as ET from collections import defaultdict import matplotlib.pyplot as plt %matplotlib inline import pprint import re import codecs import ...