Python——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.

map()函数是常用的高阶函数,第一个参数是函数,第二个参数是迭代对象,功能是对可迭代对象分别使用函数,返回map对象。
map函数是python的内置函数之一,内置函数概览详见:https://mp.csdn.net/mdeditor/90751587#
https://blog.csdn.net/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函数不带参数;
含有多个可迭代对象时,最短的结束之后,迭代停止

猜你喜欢

转载自blog.csdn.net/houhuipeng/article/details/90757515