英文文档:

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内置函数(40)——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内置函数(34)——map

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

  5. Python内置函数(40)——dir

    英文文档: dir([object]) Without arguments, return the list of names in the current local scope. With an ...

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

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

  7. Python内置函数之filter map reduce

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

  8. Python内置函数和内置常量

    Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...

  9. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

随机推荐

  1. SourceInSight自定义命令说明与应用

    1.自定义命名设置界面(Tool -> Custom Commands...) Command 显示当前选中命令的名字. 下拉列表中包含了所有的自定义命令. Run 命令行,当调用自定义命令时, ...

  2. SOUI中TaskLoop组件介绍

    SOUI是一套开源(MIT协议)的Windows平台下的DirectUI框架,它提供了大量的高效控件,也提供了很多扩展组件,目前已经持续维护近10年,在大量的项目中证明稳定可靠. GIT地址: 国内: ...

  3. 一个类,有新增元素(add)和获取元素数量(size)方法。 启动两个线程。线程1向容器中新增数据。线程2监听容器元素数量,当容器元素数量为5时,线程2输出信息并终止

    方式一: /** * 两个线程要是可见的所以要加上votalile */public class Test_01 { public static void main(String[] args) { ...

  4. 深圳同城快跑笔试题目 2 实现json字符串保存到本地硬盘

    //从给定位置读取Json文件 public static String readJson(String path){ //从给定位置获取文件 File file = new File(path); ...

  5. 3步永久性激活pycharm 亲测有效

    ----------破解后,有效时间会到2099年------------ 1.下载JAR包:此JAR包的目的就是让截获截止时间并骗过pycharm 链接:https://pan.baidu.com/ ...

  6. Mac下使用数据库将Excel数据转换存入.plist

    记录于2013/10/26   基本步骤: 1.将Excel表格另存为.csv格式 2.用类似TextWrangler工具将.csv文件转成UTF-8格式 3.使用火狐插件SQLite Manager ...

  7. 【QT】QApplication简介

    1.QApplication QApplication类管理GUI程序的控制流和主要设置,是基于QWidget的,为此特化了QGuiApplication的一些功能,处理QWidget特有的初始化和结 ...

  8. 小乌龟 git ssh配置问题解决, 没有的话执行pull push会没有权限,因为没有git的ssh

    ortoisegit 常见错误disconnected no supported authentication methods available(server sent: publickey) ht ...

  9. Simple 杂题练手记

    Problem 1 世界上最可爱的珂朵莉 时间限制:C/C++ 1秒,空间限制:C/C++ 65536K 题目描述 我永远喜欢珂朵莉~! 有两个长为n的序列a[i]与b[i] 你可以把任意不多于x个a ...

  10. textarea--去掉空格的办法

    我在初次用到textarea多行文本框时,遇到的问题是:默认出现了两行空格,如图: 代码是(错误的写法): 最后才发现,原来是: textarea标签是需要写在同一行,不能换行,正确的写法: