码迷,mamicode.com
首页 > 编程语言 > 详细

python学习-33 max和min函数的高级使用

时间:2019-07-10 15:19:14      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:max   finish   color   values   span   proc   roc   返回   ESS   

1.简单比较

age_dic={age1456:15,age2:16,xiaohong_age:12,xiaoming_age4:18,age5:10}

print(max(age_dic))              # key值进行比较。比较时一位一位比较

print(max(age_dic.values()))     # values 值比较

运行结果:

xiaoming_age4
18

Process finished with exit code 0

2.高级方法


age_dic={‘age1456‘:15,‘age2‘:16,‘xiaohong_age‘:12,‘xiaoming_age4‘:18,‘age5‘:10}

for
item in zip(age_dic.values(),age_dic.keys()): print(item) # 返回每个值 print(max(zip(age_dic.values(),age_dic.keys())))

运行结果:

(15, age1456)
(16, age2)
(12, xiaohong_age)
(18, xiaoming_age4)
(10, age5)
[18, xiaoming_age4]

Process finished with exit code 0

3.终极方法

a =[
    {name:aa,age:18},
    {name:bb,age:21},
    {name:xm,age:10}
]

print(max(a,key=lambda dic:dic[age]))

‘‘‘
就相当于:
ret = []
for item in a:
    ret.append(item[‘age‘])
print(ret)
max(ret)
‘‘‘

运行结果:

{name: bb, age: 21}

Process finished with exit code 0

 

python学习-33 max和min函数的高级使用

标签:max   finish   color   values   span   proc   roc   返回   ESS   

原文地址:https://www.cnblogs.com/liujinjing521/p/11163828.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!