源代码解释:
""
map(func, *iterables) --> map object
Make an iterator that computes the function using arguments from
each of the iterables. Stops when the shortest iterable is exhausted.
"""
map接受的参数中:
第一个参数是一个函数func,
第二个参数是此函数func的输入,其类型可以为 list、tuple、string(字符串),可以传两个list或者多个list,对应的位置的值作为func的一次输入参数。
注意 :
传入的参数是可迭代的,map将此函数的功能都映射到可迭代对象的每个值上。
例子:
def square(x, y):
return x+y
if __name__ == "__main__":
print(list(map(square, [1,2,3,4,5],[5,5,5,5,5])))#可并列
print(list(map(lambda x: x**2, [1,2,3,4,5]))) #可使用lambda函数表达式
output:
[6, 7, 8, 9, 10]
[1, 4, 9, 16, 25]