内置函数系列之 map

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

1
2
3
4
5
6
7
8
9
10
11
12
# 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]

  

posted @   王剑威  阅读(111)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示