Python官方Tutorial

Functional Programming Tools

函数化编程工具

There are three built-in functions that are very useful when used with lists: filter(), map(), and reduce().

列表有三个很有用的内建函数: filter(), map(), 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 a sequence of numbers not divisible by 2 and 3:

filter(function, sequence)返回一个包含item的序列,这些item来自sequence,而且function(item)返回true. 如果sequence是字符串或者元组, 返回的结果也是字符串或者元组;否则,他永远是列表。例如,计算一个不能被2和3整除的数字元组。

>>> 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:

map(function, sequence)在序列中的每个元素上调用function(item),返回一个列表。例如,计算一些体积:

>>> 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:

不止一个序列作为参数;函数的参数数目和传递给map函数的参数必须一致。作为map函数的参数的序列的相关项作为function的参数来调用此函数。(如果一个序列的长度大于另一个,短缺的值为None).例如:

>>> 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:

reduce(function, sequence)返回单个值。调用二元函数计算序列的前2个列表项的值,然后计算前2项之和和第三项,以此类推。例如:计算1到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.

不用使用此例定义sum():此函数常用,sum()是内建函数,就是这样哦那个做的。

转载于:https://www.cnblogs.com/shi-zheng/archive/2012/03/10/2389538.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值