📜  Python|获取字典中最大值的键

📅  最后修改于: 2022-05-13 01:54:18.646000             🧑  作者: Mango

Python|获取字典中最大值的键

给定一个字典,任务是找到具有最大值的键。
例子 :

Input : {'Audi':100, 'BMW':1292, 'Jaguar': 210000, 'Hyundai' : 88}
Output : Jaguar

Input:  {'Geeks':1900, 'for':1292, 'geek' : 88}
Output:  Geeks


方法 #5:使用 max()函数

Python3
# Python code to find key with Maximum value in Dictionary
  
# Dictionary Initialization
Tv = {'BreakingBad':100, 'GameOfThrones':1292, 'TMKUC' : 88}
 
Keymax = max(zip(Tv.values(), Tv.keys()))[1]
print(Keymax)


Python3
# Python code to find key with Maximum value in Dictionary
  
# Dictionary Initialization
Tv = {'BreakingBad':100, 'GameOfThrones':1292, 'TMKUC' : 88}
 
Keymax = max(Tv, key= lambda x: Tv[x])
print(Keymax)


Python3
# Python code to find key with
# Maximum value in Dictionary
import operator
 
# Dictionary Initialization
Car = {'Audi':100, 'BMW':1292, 'Jaguar': 210000, 'Hyundai' : 88}
 
# Getting max item
keyMax = max(Car.items(), key = operator.itemgetter(1))[0]
print(keyMax)


Python3
# Python code to find key with Maximum value in Dictionary
 
# Dictionary Initialization
Company = {'GFG':10000, 'Hashd':2292, 'Infy': 200}
 
# taking list of car values in v
v = list(Company.values())
 
# taking list of car keys in v
k = list(Company.keys())
 
print(k[v.index(max(v))])


输出:
GameOfThrones


方法 2:将 max() 与 lambda函数一起使用

Python3

# Python code to find key with Maximum value in Dictionary
  
# Dictionary Initialization
Tv = {'BreakingBad':100, 'GameOfThrones':1292, 'TMKUC' : 88}
 
Keymax = max(Tv, key= lambda x: Tv[x])
print(Keymax)
输出:
GameOfThrones


方法三:使用运算符

Python3

# Python code to find key with
# Maximum value in Dictionary
import operator
 
# Dictionary Initialization
Car = {'Audi':100, 'BMW':1292, 'Jaguar': 210000, 'Hyundai' : 88}
 
# Getting max item
keyMax = max(Car.items(), key = operator.itemgetter(1))[0]
print(keyMax)
输出:
Jaguar


方法四:

Python3

# Python code to find key with Maximum value in Dictionary
 
# Dictionary Initialization
Company = {'GFG':10000, 'Hashd':2292, 'Infy': 200}
 
# taking list of car values in v
v = list(Company.values())
 
# taking list of car keys in v
k = list(Company.keys())
 
print(k[v.index(max(v))])
输出:
GFG