12-4 map与lambda
# 12-4 map与lambda
# 看看两者的应用
list_x = [1,2,3,4,5,6,7,8]
r = map(lambda x: x*x ,list_x)
print(list(r))
# 打印结果如图1
# 再看几个稍微复杂点的例子
list_x = [1,2,3,4,5,6,7,8]
list_y = [1,2,3,4,5,6,7,8]
r = map(lambda x,y: x*x+y,list_x,list_y)
print(list(r))
# 打印结果如图2
list_x = [1,2,3,4,5,6,7,8]
list_y = [1,2,3,4,5,6,]
r = map(lambda x,y: x*x+y,list_x,list_y)
print(list(r))
# 打印结果如图2
list_x = [1,2,3,4,5,6]
list_y = [1,2,3,4,5,6,7,8]#打印结果取决于列表较短的那个
r = map(lambda x,y: x*x+y,list_x,list_y)
print(list(r))
# 打印结果如图2
1.jpg
2.jpg
网友评论