1、先看看什么是 iterable 对象

以内置的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 <= end:
        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.
这个已经介绍的很明了,
复制代码 代码如下:reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
相当于计算
复制代码 代码如下:
((((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'])

标签:
Python,map,reduce,filter

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“Python中的map、reduce和filter浅析”
暂无“Python中的map、reduce和filter浅析”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。