美文网首页
Python入门与进阶(12-4)

Python入门与进阶(12-4)

作者: buaishengqi | 来源:发表于2018-05-15 14:10 被阅读13次

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

相关文章

网友评论

      本文标题:Python入门与进阶(12-4)

      本文链接:https://www.haomeiwen.com/subject/hpiedftx.html