6、lambda匿名函数

匿名函数就是不需要显式的指定函数名

#这段代码
def calc(x,y):
    return x**y

print(calc(2,5))

#换成匿名函数
calc = lambda x,y:x**y
print(calc(2,5))

map()函数一起使用

res1 = map(lambda x:x**2,[1,5,7,8])
for i in res1:
    print(i)

# 输出
1
25
49
64

filter()函数一起使用

res2 = filter(lambda x:x%2,[i for i in range(1,11)])
for i in res2:
    print(i)

# 输出
1
3
5
7
9

猜你喜欢

转载自blog.csdn.net/vettel2018/article/details/86644572
今日推荐