Python中map()函数浅析
来源:互联网 发布:随机森林算法matlab 编辑:程序博客网 时间:2024/04/30 12:15
http://my.oschina.net/zyzzy/blog/115096
MapReduce的设计灵感来自于函数式编程,这里不打算提MapReduce,就拿python中的map()函数来学习一下。
文档中的介绍在这里:
map(function, iterable, ...)
Apply function to every item of iterable and return a list of the results. If additionaliterable 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 withNoneitems. Iffunction isNone, 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). Theiterable arguments may be a sequence or any iterable object; the result is always a list.
一点一点看:
1、对可迭代函数'iterable'中的每一个元素应用‘function’方法,将结果作为list返回。
来个例子:
>>>
def
add100(x):
...
return
x
+
100
...
>>> hh
=
[
11
,
22
,
33
]
>>>
map
(add100,hh)
[
111
,
122
,
133
]
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
]
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
)]
介绍到这里应该差不多了吧!不过还有东西可以挖掘:
stackoverflow上有人说可以这样理解map():
map
(f, iterable)
基本上等于:
[f(x)
for
x
in
iterable]
>>>
def
add100(x):
...
return
x
+
100
...
>>> list1
=
[
11
,
22
,
33
]
>>>
map
(add100,list1)
[
101
,
102
,
103
]
>>> [add100(i)
for
i
in
list1]
[
101
,
102
,
103
]
>>>
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
]
[abc(a,b,c)
for
a
in
list1
for
b
in
list2
for
c
in
list3]
但是看到结果,发现根本不是这么回事:
[
114477
,
114488
,
114499
,
115577
,
115588
,
115599
,
116677
,
116688
,
116699
,
224477
,
224488
,
224499
,
225577
,
225588
,
225599
,
226677
,
226688
,
226699
,
334477
,
334488
,
334499
,
335577
,
335588
,
335599
,
336677
,
336688
,
336699
]
result
=
[]
for
a
in
list1:
for
b
in
list2:
for
c
in
list3:
result.append(abc(abc))
map()只做了列上面的运算,而列表推导(也就是嵌套for循环)做了笛卡尔乘积。
OK,就写到这里。仅个人理解,如有差错请指正,多谢!
上面的例子有些来自于这里:
http://infohost.nmt.edu/tcc/help/pubs/python/web/map-function.html
http://stackoverflow.com/questions/10973766/understanding-the-map-function-python
- Python中map()函数浅析
- Python中map()函数浅析
- Python中map()函数浅析
- Python中map()函数浅析
- Python中map()函数浅析
- Python中map()函数浅析
- Python中map()函数浅析
- Python中map()函数浅析
- Python中map()函数浅析
- Python中map()函数浅析
- Python中map()函数浅析
- Python中map()函数浅析
- Python中map()内建函数浅析
- Python中map()函数
- python中map()函数
- Python中map函数
- Python中map函数
- python 中map函数使用
- How to use umdh to check Windows memory leak
- 20个最佳的Java集合框架面试题目
- Python xlrd、xlwt、xlutils修改Excel文件
- 全文索引 Lucene初探
- cots 常见问题解答
- Python中map()函数浅析
- 诗意回文
- 自己动手写spring(一) 使用digester
- 活动h5页面倒计时效果
- windows server 2012 standard eval评估板过期-解决方法
- Windows Caffe中MNIST数据格式转换实现
- Windows文件和文件夹定期清理
- svn的备份还原(一)
- 安装