Python内置函数(40)——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函数是一个典型的函数式编程例子。

时间: 2024-08-09 19:51:06

Python内置函数(40)——map的相关文章

Python 内置函数&amp;filter()&amp;map()&amp;reduce()&amp;sorted()

常用内置函数 Python 2.x 返回列表,Python 3.x 返回迭代器 在进行筛选或映射时,输出的结果是一个数组,需要list帮助. 如:print(list(map(lambda x:x+1, [1,2,3]))) 一.filter() --过滤.筛选 刚接触filter时 ,运行总是出现<filter object at 0x000001B68F052828> 得不到想要的数据,后来发现是因为filter的结果是一个数组, 需要 list 帮助,后来将print(f) 改为 pri

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

#内置函数zip(),将多个可迭代对象(集合等)按照顺序进行组合成tuple元祖,放在zip 对象进行存储,: #当参数为空时候,返回空 #如果 zip() 函数压缩的两个列表长度不相等,那么 zip() 函数将以长度更短的列表为准; list_t1= [1,2,3] list_t2 =['apple','orange','banana'] list_t3 = [50,60,70,80] list_t4 = (500,600,700,800) list_z1 = zip(list_t1,list

Python内置函数filter, map, reduce

filter.map.reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是Python列表方法的三架马车. 1. filter函数的功能相当于过滤器. filter函数的定义: filter(function or None, sequence) -> list, tuple, or string function是一个谓词函数,接受一个参数,返回布尔值True或False. filter函数会对序列参数sequence中的每个元素调用

python内置函数filter(),map(),reduce()笔记

'''python reduce()函数:reduce()函数会对参数序列中元素进行积累. 函数将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给reduce中的函数 function(有两个参数)先对集合中的第 1.2 个元素进行操作,得到的结果再与第三个数据用 function 函数运算,最后得到一个结果. 语法:ruduce()reduce(function,iterable,initializer)参数:function-函数,有两个参数iterable--可迭代对象init

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

python内置函数map/reduce/filter 这三个函数用的顺手了,很cool. filter()函数:filter函数相当于过滤,调用一个bool_func(只返回bool类型数据的方法)来迭代遍历每个序列中的元素. 返回bool_func结果为true的元素的序列(注意弄清楚序列是什么意思)http://blog.csdn.net/bolike/article/details/19997465序列参考</a> 如果filter参数值为None,list参数中所有为假的元 素都将被

Python基础篇【第2篇】: Python内置函数--map/reduce/filter/sorted

Python内置函数 lambda lambda表达式相当于函数体为单个return语句的普通函数的匿名函数.请注意,lambda语法并没有使用return关键字.开发者可以在任何可以使用函数引用的位置使用lambda表达式.在开发者想要使用一个简单函数作为参数或者返回值时,使用lambda表达式是很方便的.总结:处理简单逻辑,自动返回结果 语法格式: lambda parameters: expression 就相当于 def fun(args) return expression 并且lam

Python内置函数之filter map reduce

Python内置函数之filter map reduce 2013-06-04 Posted by yeho Python内置了一些非常有趣.有用的函数,如:filter.map.reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是Python列表方法的三架马车.1. filter函数的功能相当于过滤器.调用一个布尔函数bool_func来迭代遍历每个seq中的元素:返回一个使bool_seq返回值为true的元素的序列. >>>

Python补充--Python内置函数清单

Python内置函数 Python内置(built-in)函数随着python解释器的运行而创建.在Python的程序中,你可以随时调用这些函数,不需要定义.最常见的内置函数是: print("Hello World!") 在Python教程中,我们已经提到下面一些内置函数:基本数据类型 type()反过头来看看 dir()   help()    len()词典 len()文本文件的输入输出 open()循环设计 range()   enumerate()    zip()循环对象

Python 内置函数sorted()有哪些高级用法?

本文和大家分享的主要是python内置函数sorted()的相关内容,一起来看看吧,希望对大家学习python http://www.maiziedu.com/land/python/有所帮助. 1.对于 Python 内置函数 sorted() ,先拿来跟list(列表)中的成员函数 list.sort() 进行下对比.在本质上,list的排序和内建函数sorted的排序是差不多的,连参数都基本上是一样的. 2.主要的区别在于, list.sort() 是对已经存在的列表进行操作,进而可以改变