Python中map函数
来源:互联网 发布:新浪财经怎么下载数据 编辑:程序博客网 时间:2024/05/21 09:56
python中的map()函数
map(function, iterable, ...)
1.对可迭代函数'iterable'中的每一个元素应用‘function’方法,将结果作为list返回。
>> def add100(x):... return x+100... >>> hh = [11,22,33]>>> map(add100,hh)[111, 122, 133]
如果是python 3见上一篇博客。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)]
介绍到这里应该差不多了吧!不过还有东西可以挖掘:
有人说可以这样理解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]
哦,输出结果一样。原来map()就是列表推导式啊!要是这样想就错了:这里只是表面现象!再来个例子看看:>>> 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))
原来如此,若是将三个list看做矩阵的话:112233445566778899map()只做了列上面的运算,而列表推导(也就是嵌套for循环)做了笛卡尔乘积参考:http://infohost.nmt.edu/tcc/help/pubs/python/web/map-function.html
阅读全文
0 0
- 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()函数的使用
- 新闻客户端
- C++之this指针
- [树的点分治] [POJ2114] Boatherds
- SpringMVC实现服务器端推送
- Supervised Learning 002: k-Nearest Neighbor
- Python中map函数
- Server与Cilent
- 线程的分离和结合
- requirejs 教程(一)
- loadrunner 接口测试实例:天气接口,get&post
- git reset 简介
- vb.net 教程 11-1 打印组件 2 PrintDialog 2
- 计蒜客 2017 复赛 腾讯消消乐 状压dp
- 请不要滥用SharedPreference