How to get the key corresponding to the maximum value in the dictionary by python
This article is about how python gets the key corresponding to the maximum value in the dictionary. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Get the key corresponding to the maximum value in the dictionary
Sometimes it is necessary to find the key corresponding to the maximum value in the dictionary. First, find the index with the largest value in the list of all values, and then find the corresponding key from another list that stores all the keys. Alternatively, an easier way is to specify the key parameter in the max () function.
For simplicity, do not consider the situation where the maximum value may be duplicated. Similarly, you can use the min () function to find the key corresponding to the minimum value.
> model_scores = {'model_a': 100,' model_z': 1988,' model_t': 150} > > # workaround > keys, values = list (model_scores.keys ()), list (model_scores.values ()) > keys [values.index (max (values))]' model_z' > # one-line > max (model_scores, key=model_scores.get) 'model_z' Thank you for reading! This is the end of the article on "how to get the key corresponding to the maximum value in the dictionary". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!