先看手册中定义:
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.
map()函数是常用的高阶函数,第一个参数是函数,第二个参数是迭代对象,功能是对可迭代对象分别使用函数,返回map对象。
map函数是python的内置函数之一,内置函数概览详见:https://mp.youkuaiyun.com/mdeditor/90751587#
https://blog.youkuaiyun.com/oaa608868/article/details/53506188
实例:
a= [1, 2, 3]
def f(x):
return x**2
print(map(f,a))#output:map object at 0x0000024C36EC1C50
print(list(map(f,a)))#使用list实现强制转化,output:[1,4,9]
此时f函数不带参数;
含有多个可迭代对象时,最短的结束之后,迭代停止
本文详细解析了Python内置函数map的工作原理及应用实例,包括如何将函数应用于迭代对象的每个元素,以及处理多个迭代对象时的行为特性。
1095

被折叠的 条评论
为什么被折叠?



