lambda表达式应用

li = [1,2,-1,5,-2,6,7]
#max(最大的)
s1 = max(li,key=lambda x:x)
print(s1) #7
#min(最小的)
s2 = min(li,key=lambda x:x)
print(s2) # -2
#map(映射)
s3 = map(lambda x,y:x*y,li,li)
print(list(s3)) #[1, 4, 1, 25, 4, 36, 49]
# filter(过滤)
s4 = filter(lambda x:x>3,li)
print(list(s4)) #[5, 6, 7]
#sorted(排序)
s5 = sorted(li,key=lambda x:x)
print(list(s5)) #[-2, -1, 1, 2, 5, 6, 7]
#reduce(累积)
s6 = reduce(lambda x,y:x+y,li)
print(s6) #18

 

posted @ 2019-09-16 20:28  Primrose  阅读(164)  评论(0编辑  收藏  举报