Get multiple maximum keys in Python dictionary type

Thing you want to do

Dictionary with multiple maximum values

d = {
  'a': 1,
  'b': 2,
  'c': 3,
  'd': 3,
  'e': 2
}

For, I want to process to get multiple keys ([c, d] in this case)

Implementation (Added on July 24, 2017)

From @shiracamus

Isn't it simpler not to use numpy?

by_shiracamus.py


d = {
  'a': 1,
  'b': 2,
  'c': 3,
  'd': 3,
  'e': 2
}

#Find the key whose value is max
max_val = max(d.values())
keys_of_max_val = [key for key in d if d[key] == max_val]

print(keys_of_max_val)

beautiful! keys_of_max_val = [key for key, val in d.items () if val == max_val] I like the look, but what about the speed ... Let's check it even in your free time ...

The previous one

import numpy as np

d = {
  'a': 1,
  'b': 2,
  'c': 3,
  'd': 3,
  'e': 2
}

#Get key with ndarray
keys = np.array([a[0] for a in d.items()])

#Get value with ndarray
values = np.array([a[1] for a in d.items()])

#Find the key whose value is max
keys_of_max_val = keys[values == values.max()]

print(keys_of_max_val)

I'm not used to the inclusion notation!

did it

['c' 'd']

Impressions

When there is only one maximum value

key_of_max_val = max(d.items(), key=(lambda x: x[1]))[0]

Even though you can get it with, if there are many maximum values, it will become troublesome as soon as possible.

Recommended Posts

Get multiple maximum keys in Python dictionary type
When specifying multiple keys in python sort
How to get dictionary type elements of Python 2.7
Get date in Python
[Python Tips] How to retrieve multiple keys with the maximum value from the dictionary
Convenient argument passing technique in Python (tuple, dictionary type)
Get YouTube Comments in Python
Multiple regression expressions in Python
Create a dictionary in Python
Get last month in python
Get Terminal size in Python
Explicitly get EOF in python
Avoid KeyError in python dictionary
Avoid multiple loops in Python
Get Evernote notes in Python
Prohibit multiple launches in python
Python> dictionary> get ()> optional value
Get Japanese synonyms in Python
Get the value of a specific key in a list from the dictionary type in the list with Python
Get the MIME type in Python and determine the file format
How to get all the keys and values in the dictionary
Extract multiple list duplicates in Python
Get Leap Motion data in Python.
[Python] Show multiple windows in Tkinter
Function argument type definition in python
Get data from Quandl in Python
Get the desktop path in Python
Dynamically load json type in python
Get the script path in Python
Type specified in python. Throw exceptions
Get, post communication memo in Python
Statistical test (multiple test) in Python: scikit_posthocs
Get the desktop path in Python
Get the host name in Python
Delete multiple elements in python list
Python for super beginners Python # dictionary type 2 for super beginners
Get started with Python in Blender
Project Euler # 4 "Maximum Palindrome" in Python
Handle multiple python versions in one jupyter
Get additional data in LDAP with python
Python> dictionary> values ()> Get All Values by Using values ()
Project Euler # 3 "Maximum Prime Factors" in Python
Get Suica balance in Python (using libpafe)
Project Euler # 11 "Maximum Product in Grid" in Python
Send email to multiple recipients in Python (Python 3)
Type annotations for Python2 in stub files!
Dictionary type 2
Get Google Fit API data in Python
Dictionary type 1
How to get a stacktrace in python
Get Youtube data in Python using Youtube Data API
Python / dictionary> setdefault ()> Add if not in dictionary
Hash in Perl is a dictionary in Python
Process multiple lists with for in Python
[python] Get Twitter timeline for multiple users
Get battery level from SwitchBot in Python
[Introduction to Udemy Python3 + Application] 24. Dictionary type
Get a token for conoha in python
Get Started with TopCoder in Python (2020 Edition)
Python dictionary
Solve the maximum subarray problem in Python