Python内置函数(34)——map
英文文档:
map(function, iterable, ...)- Return an iterator that applies function to every item of iterable, yielding 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. With multiple iterables, the iterator stops when the shortest iterable is exhausted. For cases where the function inputs are already arranged into argument tuples, see
itertools.starmap(). - 使用指定的方法去作用传入的每个可迭代对象的元素,生成新的可迭代对象
- 说明:
- 1. 函数接受一个函数类型参数、一个或者多个可迭代对象参数,返回一个可迭代器,此迭代器中每个元素,均是函数参数实例调用可迭代对象后的结果。
>>> a = map(ord,'abcd')
>>> a
<map object at 0x03994E50>
>>> list(a)
[97, 98, 99, 100]
2. 当传入多个可迭代对象时,函数的参数必须提供足够多的参数,保证每个可迭代对象同一索引的值均能正确传入函数。
>>> a = map(ord,'abcd')
>>> list(a)
[97, 98, 99, 100]
>>> a = map(ord,'abcd','efg') # 传入两个可迭代对象,所以传入的函数必须能接收2个参数,ord不能接收2个参数,所以报错
>>> list(a)
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
list(a)
TypeError: ord() takes exactly one argument (2 given) >>> def f(a,b):
return a + b
>>> a = map(f,'abcd','efg') # f函数可以接受2个参数
>>> list(a)
['ae', 'bf', 'cg']
3. 当传入多个可迭代对象时,且它们元素长度不一致时,生成的迭代器只到最短长度。
>>> def f(a,b):
return a + b >>> a = map(f,'abcd','efg') # 选取最短长度为3
>>> list(a)
['ae', 'bf', 'cg']
4. map函数是一个典型的函数式编程例子。
Python内置函数(34)——map的更多相关文章
- Python 内置函数&filter()&map()&reduce()&sorted()
常用内置函数 Python 2.x 返回列表,Python 3.x 返回迭代器 在进行筛选或映射时,输出的结果是一个数组,需要list帮助. 如:print(list(map(lambda x:x+1 ...
- python 内置函数zip,map,三元,lambda表达式
#内置函数zip(),将多个可迭代对象(集合等)按照顺序进行组合成tuple元祖,放在zip 对象进行存储,: #当参数为空时候,返回空 #如果 zip() 函数压缩的两个列表长度不相等,那么 zip ...
- Python内置函数filter, map, reduce
filter.map.reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是Python列表方法的三架马车. 1. filter函数的功能相当 ...
- Python内置函数(40)——map
英文文档: map(function, iterable, ...) Return an iterator that applies function to every item of iterabl ...
- Python内置函数(34)——filter
英文文档: filter(function, iterable) Construct an iterator from those elements of iterable for which fun ...
- Python内置函数(34)——isinstance
英文文档: isinstance(object, classinfo) Return true if the object argument is an instance of the classin ...
- [python基础知识]python内置函数map/reduce/filter
python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法) ...
- Python内置函数之filter map reduce
Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...
- python内置函数详细介绍
知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档: https: ...
随机推荐
- 清除input[type=number]的默认样式
input[type=number] { -moz-appearance:textfield; } input[type=number]::-webkit-inner-spin-button, inp ...
- WPF-悬浮窗(类似于360)
boss要求开发一个类似于360的悬浮窗,如下图所示: 目前采用的是wpf做的客户端,之前有个winform的项目,我参考了下,完成了wpf版的悬浮窗. Height=" WindowSta ...
- UML 中extend和include的区别
在UML用例图中有两种关系——包含和扩展,容易混淆,下面通过一张表来区别一下这两种关系.
- 15.MySQL(三)
索引类型 先创建表 mysql> CREATE TABLE test( -> id INT, -> username VARCHAR(16), -> city VARCHAR( ...
- html学习第一弹の常用标签的归类
HTML初步学习: 行内元素:只占据他对应标签的边框所包含的空间,默认横向排布. 块级元素:块级元素占据其父元素(容器)的整个空间,因此创建了一个块,通常浏览器会在块级元素前后另起一行,默认竖向排布. ...
- win8快捷键
win+Q/S搜索所有位置 win+W搜索设置 win+E文件资源管理器 win+R运行 win+T选中第一个应用程序(不确定) win+U轻松使用设置中心 win+I设置 win+P投影 win+D ...
- AvalonJS前端开发源码
avBody = avalon.define("avBody", function (vm) { vm.Address = "";//地址 vm.BrandMo ...
- JAVAEE——BOS物流项目08:配置代理对象远程调用crm服务、查看定区中包含的分区、查看定区关联的客户
1 学习计划 1.定区关联客户 n 完善CRM服务中的客户查询方法 n 在BOS项目中配置代理对象远程调用crm服务 n 调整定区关联客户页面 n 实现定区关联客户 2.查看定区中包含的分区 n 页面 ...
- spring boot 启动问题
spring boot启动报错误 Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.co ...
- 【Python】 子进程创建与使用subprocess
subprocess *****本文参考了Vamei大神的http://www.cnblogs.com/vamei/archive/2012/09/23/2698014.html 运用subproce ...