The Python implementation finds the key corresponding to the dictionary’s maximum value
Define a dictionary as follows:
[Python]
Plain text view
Copy the code
?
1
|
my_dict = { 'a' : 5 . 'b' : 2 . 'c' : 6 . 'd' : 1 . 'e' : 3 . 'f' : 4 }
|
1. sorted
[Python]
Plain text view
Copy the code
?
1
2
|
ret = sorted (my_dict,key = lambda x:my_dict[x], reverse = True ) [ 0 ]
print (ret)
|
2. max
[Python]
Plain text view
Copy the code
?
1
2
|
ret = max (my_dict, key = lambda x:my_dict[x])
print (ret)
|
The above are the two methods that I usually summarize. The other implementation methods are welcome to comment
For more free technical information: annalin1203