Python中的map、reduce和filter浅析
以内置的max函数为例子,查看其doc:
>>> print max.__doc__
max(iterable[, key=func]) -> value
max(a, b, c, ...[, key=func]) -> value
With a single iterable argument, return its largest item.
With two or more arguments, return the largest argument.
在max函数的第一种形式中,其第一个参数是一个 iterable 对象,既然这样,那么哪些是 iterable 对象呢?
>>> max('abcx')
>>> 'x'
>>> max('1234')
>>> '4'
>>> max((1,2,3))
>>> 3
>>> max([1,2,4])
>>> 4
我们可以使用yield生成一个iterable 对象(也有其他的方式):
def my_range(start,end):
''' '''
while start yield start
start += 1
执行下面的代码:
for num in my_range(1, 4):
print num
print max(my_range(1, 4))
将输出:
1
2
3
4
4
2、map
在http://docs.python.org/2/library/functions.html#map中如此介绍map函数:
map(function, iterable, ...)
Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended with None items. If function is None, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.
map函数使用自定义的function处理iterable中的每一个元素,将所有的处理结果以list的形式返回。例如:
def func(x):
''' '''
return x*x
print map(func, [1,2,4,8])
print map(func, my_range(1, 4))
运行结果是:
[1, 4, 16, 64]
[1, 4, 9, 16]
也可以通过列表推导来实现:
print [x*x for x in [1,2,4,8]]
3、reduce
在http://docs.python.org/2/library/functions.html#reduce中如下介绍reduce函数:
reduce(function, iterable[, initializer])
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned.
这个已经介绍的很明了,
相当于计算
((((1+2)+3)+4)+5)
而:
reduce(lambda x, y: x+y, [1, 2, 3, 4, 5],6)
相当于计算
(((((6+1)+2)+3)+4)+5)
4、filter
在http://docs.python.org/2/library/functions.html#filter中如下介绍filter函数:
filter(function, iterable)
Construct a list from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If iterable is a string or a tuple, the result also has that type; otherwise it is always a list. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.
Note that filter(function, iterable) is equivalent to [item for item in iterable if function(item)] if function is not None and [item for item in iterable if item] if function is None.
参数function(是函数)用于处理iterable中的每个元素,如果function处理某元素时候返回true,那么该元素将作为list的成员而返回。比如,过滤掉字符串中的字符a:
def func(x):
''' '''
return x != 'a'
print filter(func, 'awake')
运行结果是:
wke
这也可以通过列表推导来实现:
print ''.join([x for x in 'awake' if x != 'a'])
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
相关文章
相关视频
专题推荐
-
独孤九贱-php全栈开发教程
全栈 170W+
主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门
-
玉女心经-web前端开发教程
入门 80W+
主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门
-
天龙八部-实战开发教程
实战 120W+
主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习
推荐阅读
-
python中的实例方法、静态方法、类方法、类变量和实例变量浅析
-
Python中的高级函数map/reduce使用实例
-
Python中的引用和拷贝浅析
-
Python中的map、reduce和filter浅析
-
浅析Python3中的bytes和str类型
-
Python中的map()函数和reduce()函数的用法
-
简单介绍Python中的filter和lambda函数的使用
-
浅析JS中的 map, filter, some, every, forEach, for in, for of 用法总结
-
Python之lambda匿名函数及map和filter的用法
-
python入门语法(函数参数、迭代器、生成器、装饰器、函数式变成、map/reduce、filter、sorted、继承和多态)
网友评论
文明上网理性发言,请遵守 新闻评论服务协议
我要评论