Python map() 函数

功能:根据提供的函数对指定序列做映射

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

语法:map(function, iterable, ...)

参数:function:函数

          iterable:一个或多个可迭代序列

实例:

def square(x):
return x ** 2

list(map(square(x), [1,2,3,4,5]))

输出:[1, 4, 9, 16, 25]

也可使用 lambda 匿名函数:

list(map(lambda x: x ** 2, [1,2,3,4,5]))

输出:[1, 4, 9, 16, 25]

# 提供了两个列表,对相同位置的列表数据进行相加

>>> map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])

[3, 7, 11, 15, 19]

原文地址:https://www.cnblogs.com/demo-deng/p/7505899.html