内置函数系列之 map

map(映射函数)
语法:
map(函数,可迭代对象)
可以对可迭代对象中的每一个元素,分别执行函数里的操作

# 1.计算每个元素的平方
lst = [1,2,3,4,5]
lst_new = map(lambda x:x ** 2,lst)
print(list(lst_new))
# 结果:[1, 4, 9, 16, 25]

# 2.计算两个列表中相同位置的和
lst1 = [1,2,3,4,5]
lst2 = [1,2,3,4,5]
lst_new = map(lambda x,y:x + y,lst1,lst2)
print(list(lst_new))
# 结果:[2, 4, 6, 8, 10]

  

原文地址:https://www.cnblogs.com/594504110python/p/9352832.html