码迷,mamicode.com
首页 > 编程语言 > 详细

python中的map函数

时间:2015-09-06 16:02:58      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

1、对可迭代函数‘iterable‘中的每一个元素应用‘function’方法,将结果作为list返回。

 

来个例子:

>>> def add100(x):

...     return x+100

...

>>> hh = [11,22,33]

>>> map(add100,hh)

[111, 122, 133]

就像文档中说的:对hh中的元素做了add100,返回了结果的list。

 

2、如果给出了额外的可迭代参数,则对每个可迭代参数中的元素‘并行’的应用‘function’。(翻译的不好,这里的关键是‘并行’)

>>> def abc(a, b, c):

...     return a*10000 + b*100 + c

...

>>> list1 = [11,22,33]

>>> list2 = [44,55,66]

>>> list3 = [77,88,99]

>>> map(abc,list1,list2,list3)

[114477, 225588, 336699]

看到并行的效果了吧!在每个list中,取出了下标相同的元素,执行了abc()。

 

3、如果‘function‘给出的是‘None’,自动假定一个‘identity’函数(这个‘identity’不知道怎么解释,看例子吧)

 

>>> list1 = [11,22,33]

>>> map(None,list1)

[11, 22, 33]

>>> list1 = [11,22,33]

>>> list2 = [44,55,66]

>>> list3 = [77,88,99]

>>> map(None,list1,list2,list3)

[(11, 44, 77), (22, 55, 88), (33, 66, 99)]

python中的map函数

标签:

原文地址:http://www.cnblogs.com/CaptainJ/p/4785734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!