Skip to content

Commit fb94366

Browse files
committed
add 07-function README 🙏
1 parent cff19a3 commit fb94366

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

04-generator-and-iterator/iterator_visit.py

-1
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,3 @@ def flatten(items, ignore_types=(str, bytes)):
116116
items = ['Dave', 'Paula', ['Thomas', 'Lewis']]
117117
for x in flatten(items):
118118
print(x)
119-

07-function/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
1. `anonymous.py`:匿名函数
2+
2. `parameter.py`:传参的艺术

07-function/anonymous.py

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
-----------
1111
@ 佛祖保佑,永无BUG--
1212
'''
13+
import math
1314

1415
# 匿名函数
1516
# 冒号前是参数,冒号后是返回的结果
@@ -39,3 +40,15 @@
3940
b = lambda y, x=x: x + y
4041
print(a(10))
4142
print(b(10))
43+
44+
# 多个数值的比较并排序
45+
center = (4.2, 6.7)
46+
points = [ (1, 2), (3, 4), (5, 6), (7, 8) ]
47+
48+
def distance(p1, p2):
49+
x1, y1 = p1
50+
x2, y2 = p2
51+
return math.hypot(x2 - x1, y2 - y1)
52+
53+
points.sort(key=lambda p: distance(center, p))
54+
print(points)

07-function/parameter.py

-1
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,3 @@ def distance(p1, p2):
9292
print(points)
9393
# 等价
9494
points.sort(key=lambda p: distance(center, p))
95-

0 commit comments

Comments
 (0)