map()函数

1. 函数语法:

map(function,iterable)

function:函数

iterable:一个或者多个序列

 

2. map()会根据提供的函数对指定的序列做映射

第一个参数function()以参数序列中的每一个元素调用function函数,返回包含每次function函数返回值的新列表

>>> def square(x):
>>> return x ** 2
>>> map(square,[1,2,3,4,5])
<map at 0xbd26f28>
>>> list(map(square,[1,2,3,4,5]))
[1, 4, 9, 16, 25]
>>> def add(x,y,z):
>>> return x + y + z
>>> list1 = [1,2,3]
>>> list2 = [1,2,3,4]
>>> list3 = [1,2,3,4,5]
>>> res = map(add,list1,list2,list3) #有截断功能,按最短的算
>>> print(list(res)) #1+1+1,2+2+2,3+3+3
[3, 6, 9]

 

posted @ 2020-10-03 16:35  J-Moment  阅读(155)  评论(0)    收藏  举报