程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python max min function

編輯:Python
# max For maximum min For the minimum 
print(max([99, 10, 1, 5, 6, 3]))
""" key Usage of parameters : min(*args, key=None): key Is equal to the function name , Note that the function name should not be bracketed ,min Each element of the iteratable object is automatically passed as an argument to key The formal parameters of the function , And then according to key Compare the size of the return value of the function of , The final return is the smallest passed in argument . """
def func(x):
return x[1]
l = [(' Monday ', 300), (' Tuesday ', 200), (' Wednesday ', 99)]
print(min(l, key=func)) # (' Wednesday ', 99)
print(min(l, key=lambda x: func(x))) # (' Wednesday ', 99)
print(min(l, key=lambda x: func(x))[1]) # 99
dic = {
'iphone':5999,'xiaomi':3999, 'oppo':3599}
# By default, the size is compared according to the key , Return key 
print(min(dic)) # iphone
# Compare sizes by value , Return key 
print(min(dic, key=lambda x:dic[x])) # oppo

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved