最大最小值

示例一:
MAXIMUM = lambda x,y :  (x > y) * x + (x < y) * y    #注意(x > y) 返回的是0或者1
MINIMUM = lambda x,y :  (x > y) * y + (x < y) * x
a = 10
b = 20
print 'The largar one is %d' % MAXIMUM(a,b)
print 'The lower one is %d' % MINIMUM(a,b)
 
示例二:
a = 10
b = 20
print 'The largar one is %d' % max(a,b)  #注意不支持大写的MAX/MIN
print 'The lower one is %d' % min(a,b)
 
 
 
max/min用法:
 
普通用法:
a = 10
b = 20
print 'The largar one is %d' % max(a,b,5)
print 'The lower one is %d' % min(a,b,5)
输出:
The largar one is 20
The lower one is 5
 
 
数组提取:
a=[1,2,35]
print 'The largar one is %d' % max(a)
print 'The lower one is %d' % min(a)
输出:
The largar one is 35
The lower one is 1
 
 
含key用法:
max(iterable, key, default) 求迭代器的最大值,其中iterable 为迭代器,max会for i in … 遍历一遍这个迭代器,然后将迭代器的每一个返回值当做参数传给key=func 中的func(一般用lambda表达式定义) ,然后将func的执行结果传给key,然后以key为标准进行大小的判断。
d1 = {'name': 'egon', 'price': 100}
d2 = {'name': 'rdw', 'price': 666}
d3 = {'name': 'zat', 'price': 1}
l1 = [d1, d2, d3]
a = max(l1, key=lambda x: x['name'])
print(a)
b = max(l1, key=lambda x: x['price'])
print(b)
输出:
{'price': 1, 'name': 'zat'}
{'price': 666, 'name': 'rdw'}
 
 
posted @ 2019-10-29 08:56  数之美  阅读(341)  评论(0编辑  收藏  举报