获取字典中values值中最大的数,返回对应的keys
作者:互联网
1.字典中键值对的获取
print(data.values()) # 查看字典的值 print(data.keys()) # 查看字典的key
2.对字典中的值进行排序
sorted(data.values())
sorted(data.values(),reverse=True) # 倒序
3.实操练习:获取字典中values值中最大的数,返回对应的keys
def best_stock(data: dict) -> str: li = sorted(data.values(),reverse=True) for i in data.keys(): if data[i] == li[0]: return i if __name__ == '__main__': print(best_stock({"CHINESE": 10.0, "MATH": 390.2, "ENGLISH": 1.2})) print(best_stock({"CHINESE": 10.0, "MATH": 390.2, "ENGLISH": 1.2})) print(best_stock({"CHINESE": 91.1, "MATH": 1.01, "ENGLISH": 120.9}))
标签:__,keys,values,print,值中,data,best,字典 来源: https://www.cnblogs.com/lxxduang/p/16461701.html