-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmax函数详解.py
More file actions
45 lines (36 loc) · 1018 Bytes
/
max函数详解.py
File metadata and controls
45 lines (36 loc) · 1018 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
'''
描述
max() 方法返回给定参数的最大值,参数可以为序列。
语法
以下是 max() 方法的语法:
max( x, y, z, .... )
参数
x -- 数值表达式。
y -- 数值表达式。
z -- 数值表达式。
返回值
返回给定参数的最大值。
'''
print ("max(80, 100, 1000) : ", max(80, 100, 1000))
print ("max(-20, 100, 400) : ", max(-20, 100, 400))
print ("max(-80, -20, -10) : ", max(-80, -20, -10))
print ("max(0, 100, -400) : ", max(0, 100, -400))
'''
输出结果:
max(80, 100, 1000) : 1000
max(-20, 100, 400) : 400
max(-80, -20, -10) : -10
max(0, 100, -400) : 100
'''
#1.传入的多个参数的最大值
print(max(1,2,3,4)) #输出 4
#2.传入可迭代对象时,取其元素最大值
s='12345'
print(max(s)) #输出 5
#3.传入命名参数key,其为一个函数,用来指定取最大值的方法
s = [
{'name': 'sumcet', 'age': 18},
{'name': 'bbu', 'age': 11}
]
a = max(s, key=lambda x: x['age'])
print(a) #输出 {'name': 'sumcet', 'age': 18}