Skip to content

Commit 7159c6d

Browse files
authored
练习函数式编程的匿名函数
1 parent d6e7af9 commit 7159c6d

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

test12.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 练习函数式编程的匿名函数
2+
from functools import reduce
3+
4+
5+
# 用reduce来求一个列表的和
6+
l = [x for x in range(11)]
7+
# lambda的就是匿名函数,冒号前的表示参数,冒号后的表达式的结果为返回值
8+
sum = reduce(lambda x, y: x + y, l)
9+
print(sum)
10+
11+
# 还可以将匿名函数赋值给变量
12+
f = lambda x: x * x
13+
print(f(5))
14+
print(f(8))
15+
16+
17+
# 作业:改造代码
18+
L = list(filter(lambda x: x % 2 == 1,range(1,20)))
19+
print(L)

0 commit comments

Comments
 (0)