Python map() function
函数的作用是:
将给定函数应用于给定iterable(list、tuple等)的每一项后,返回结果的map对象(迭代器)。
语法:
map(fun, iter)
参数:
fun : 函数作用用于每个iter项
iter : 必须是可迭代的
Example 1
In [1]: def addition(n):
...: return n + n
...:
In [2]: numbers = (1, 2, 3, 4)
...: result = map(addition, numbers)
...: print(list(result))
[2, 4, 6, 8]
Example 2 通过lamda表达式实现
In [3]: numbers = (1, 2, 3, 4)
...: result = map(lambda x: x + x, numbers)
...: print(list(result))
[2, 4, 6, 8]
Example 3 实现两个list 相加
In [4]: numbers1 = [1, 2, 3]
...: numbers2 = [4, 5, 6]
...:
...: result = map(lambda x, y: x + y, numbers1, numbers2)
...: print(list(result))
[5, 7, 9]
参考:
https://www.geeksforgeeks.org/python-map-function/?ref=leftbar-rightbar
不要小瞧女程序员