1.源码
def max(*args, key=None): # known special case of max
"""
max(iterable, *[, default=obj, key=func]) -> value
max(arg1, arg2, *args, *[, key=func]) -> value
With a single iterable argument, return its biggest item. The
default keyword-only argument specifies an object to return if
the provided iterable is empty.
With two or more arguments, return the largest argument.
"""
pass
2.初级使用
tem = max(1,2,4)
print(tem)
#迭代
s = [1,3,8,9,2]
print(max(s))
3.中级使用:key属性的使用
当key不为空时,以key函数对象为判断标准
#找出一组数中绝对值最大的数
a = [-9,2,-3,12]
tem = max(a, key = lambda x : abs(x))
print(tem)
4.高级使用:找出字典中值最大的那组数据
#如果有一组商品,其名称和价格都存在一个字典中,可以用下面的方法快速找到价格最贵的那组商品:
prices = {
'A':123,
'B':450.1,
'C':12,
'E':444,
}
#在对字典进行数据处理时,默认只处理key,不对value进行处理
#先用zip将key和value反转,在进行数据操作找出最大的价格
max_price = max(zip(prices.values(),prices.keys())) #将键值对转换成元组输出
print (max_price[0])
#输出值最大的键
prices = {
'A':123,
'B':450.1,
'C':12,
'E':444,
}
print(max(prices, key = lambda k:prices[k])) #值最大的键“B”,函数表示prices的值