Python中内置函数map的使用

其实感觉map和filter的功能很类似,都是处理序列,处理序列。

 
 
"""
map() 会根据提供的函数对指定序列做映射。
第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。
"""

def
add(x): return x+1 mylist = list(map(add,[1,2,3,4,5,6])) print(mylist)

>>[2, 3, 4, 5, 6, 7]
# 提供了两个列表,对相同位置的列表数据进行相加 mylists = list(map(lambda x, y: x + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10])) print(mylists)
>>[3, 7, 11, 15, 19]

猜你喜欢

转载自www.cnblogs.com/wxzbk/p/10985691.html