Skip to content

Commit fcb6317

Browse files
author
bianxiaokai
committed
feat: Lambda.py
1 parent 4b123bb commit fcb6317

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

.vscode/settings.json

Lines changed: 0 additions & 6 deletions
This file was deleted.

Lambda.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 当我们临时需要匿名函数时,我们使用lambda函数。
2+
3+
# 在Python中,我们通常将其用作高阶函数的参数(该函数将其他函数作为arguments)。Lambda函数可以与filter(),map()等内置函数一起使用。
4+
5+
my_list = [1, 5, 4, 6, 8, 11, 3, 12]
6+
7+
new_list = list(filter(lambda x: (x % 2 == 0), my_list))
8+
9+
print(new_list)

sorted.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,17 @@
11
print(sorted(['bob', 'about', 'Zoo', 'Credit'], key=str.lower, reverse=False))
2+
L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
3+
4+
5+
def by_name(t):
6+
return t[0].lower()
7+
8+
9+
def by_score(t):
10+
return t[1]
11+
12+
13+
L2 = sorted(L, key=by_name)
14+
print('L2:', L2)
15+
16+
L3 = sorted(L, key=by_score)
17+
print('L3:', L3)

0 commit comments

Comments
 (0)