英文文档:

map(functioniterable...)
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的更多相关文章

  1. Python 内置函数&filter()&map()&reduce()&sorted()

    常用内置函数 Python 2.x 返回列表,Python 3.x 返回迭代器 在进行筛选或映射时,输出的结果是一个数组,需要list帮助. 如:print(list(map(lambda x:x+1 ...

  2. python 内置函数zip,map,三元,lambda表达式

    #内置函数zip(),将多个可迭代对象(集合等)按照顺序进行组合成tuple元祖,放在zip 对象进行存储,: #当参数为空时候,返回空 #如果 zip() 函数压缩的两个列表长度不相等,那么 zip ...

  3. Python内置函数filter, map, reduce

    filter.map.reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是Python列表方法的三架马车. 1. filter函数的功能相当 ...

  4. Python内置函数(40)——map

    英文文档: map(function, iterable, ...) Return an iterator that applies function to every item of iterabl ...

  5. Python内置函数(34)——filter

    英文文档: filter(function, iterable) Construct an iterator from those elements of iterable for which fun ...

  6. Python内置函数(34)——isinstance

    英文文档: isinstance(object, classinfo) Return true if the object argument is an instance of the classin ...

  7. [python基础知识]python内置函数map/reduce/filter

    python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法) ...

  8. Python内置函数之filter map reduce

    Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对 ...

  9. python内置函数详细介绍

    知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档:    https: ...

随机推荐

  1. Spring(十二)Spring之事务

    java中事务是什么? 事务是访问数据库的一个操作序列,DB应用系统通过事务集来完成对数据的存取. 事务必须遵循4个原则,即常说的 ACID A,Automicity,原子性,即事务要么被全部执行,要 ...

  2. 微信小程序基于腾讯云对象存储的图片上传

    在使用腾讯云对象存储之前,公司一直使用的是传统的FTP的上传模式,而随着用户量的不断增加,FTP所暴露出来的问题也越来越多,1.传输效率低,上传速度慢.2.时常有上传其他文件来攻击服务器,安全上得不到 ...

  3. Java中常见的URL问题及解决方案

    URL无处不在,不过似乎开发人员并没有真正地理解它们,因为在Stack Overflow上经常看到有人在问如何正确的创建一个URL.想知道URL语法是如何工作的,可以看下兄弟连教育总结的这篇文章,非常 ...

  4. Maven-03: 优化依赖

    已解析依赖: Maven会自动解析项目的直接依赖和传递性依赖,并且根据规则正确判断每个依赖的范围,对于一些依赖冲突,也能进行调节,以确保任何一个构件只有唯一的版本在依赖中存在.在这些工作之后,最后得到 ...

  5. Redis相关命令

    一.命令示例 1. KEYS/RENAME/DEL/EXISTS/MOVE/RENAMENX: #在Shell命令行下启动Redis客户端工具. /> redis-cli #清空当前选择的数据库 ...

  6. ELK+filebeat、kafka、zookeeper搭建文档

    系统:centos 6.5 JDK:1.8 Elasticsearch-6.0.0Logstash-6.0.0kibana-6.0.0zookeeper-3.5.3kafka_2.12-1.0.0fi ...

  7. 源码实现 --> strcmp

    比较字符串大小 函数 int strcmp(const char *string1, const char *string2); 比较字符串string1和string2大小. 返回值< 0,  ...

  8. 源码实现 --> strcpy

    拷贝字符串到目标字符串 函数 char *strcpy(char *strDestination, const char *strSource); 复制源串strSource到目标串strDestin ...

  9. Eclipse中的所有快捷键列表

    Eclipse中的所有快捷键列表: Ctrl+1 快速修复(最经典的快捷键,就不用多说了) Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加) Ctrl+Alt+↑ 复制 ...

  10. mysql新手入门随笔

    1.启动/关闭服务器 第一种方法:通过Notifier 第二种方法: 通过Windows自带的服务管理:计算机右键选择管理弹出框选择"服务和应用程序"里的服务列表,从列表中找到My ...