max()的key的运用

class Person:
def __init__(self,age):
self.age = age

a = Person(5)
s = Person(1)
d = Person(2)
z = Person(7)
x = Person(88)
c = Person(55)
v = Person(3)
b = Person(6)
obj_list = [a,s,d,z,x,c,v,b]
def func(obj):
return obj.age
k = max(obj_list,key = func)
print(k.age)

f = max(obj_list,key = lambda obj : obj.age)
print(f.age)
posted @ 2020-02-15 11:53  my_Sunday  阅读(512)  评论(0编辑  收藏  举报