Python中map()函数用法-对列表中每个元素做相同操作,并返回list
2016-11-20 12:04
1226 查看
MapReduce的设计灵感来自于函数式编程,这里不打算提MapReduce,就拿python中的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 withNoneitems. If function 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). The iterable arguments may be a sequence or any iterable object; the result is always a list.
一点一点看:
1、对可迭代函数'iterable'中的每一个元素应用‘function’方法,将结果作为list返回。
来个例子:
[code]
文档中的介绍在这里:
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 withNoneitems. If function 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). The iterable 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]
>>> 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)]
>>> 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]
[code]
相关文章推荐
- python的list由多个tuple组成,如何返回每个tuple的每个元素
- Python3 列表List或元组tuple中元素分解(赋值操作)
- Python列表list内建函数用法实例分析【insert、remove、index、pop等】
- python对list中的每个元素进行某种操作的方法
- PHP array_map用法,遍历数组每个元素执行某函数
- python中对二维数组每个元素进行相同操作,以平方为例
- python list(列表)操作用法总结
- python对list中的每个元素进行某种操作
- python list(列表)操作用法总结
- python中List的sort方法(或者sorted内建函数)的用法
- python 中两个list是否有相同元素的办法
- C++ 关联容器之map插入相同键元素与查找元素操作
- python中List的sort方法(或者sorted内建函数)的用法
- python中List的sort方法(或者sorted内建函数)的用法
- python中List的sort方法(或者sorted内建函数)的用法
- Holmos自动化测试入门学习(十)------list列表元素操作
- Dive Into Python 学习记录1-函数/模块导入/字典/列表/元组/字符串分割、连接、格式化/映射list/
- Python 列表 list 数据类型创建list、添加元素和删除元素
- Effective STL: Item 21:永远让比较函数对相同元素返回false
- 【Python】-列表LIST函数实例