[PYTHON] Sort dict in dict (dictionary in dictionary) with a specific key

dict in dict sort memo

#Number of team home runs per stadium
homerun_dict = {
    'tokyo_dome': {'home_team': 10, 'hanshin': 8, 'hiroshima': 20},
    'koshien': {'home_team': 20, 'kyojin': 18, 'hiroshima': 12},
    'zoomzoom': {'home_team': 30, 'kyojin': 6, 'hanshin': 30},
}

homerun_dict_sorted = sorted(homerun_dict.items(), key=lambda x:x[1]['home_team'], reverse=True)

print(homerun_dict_sorted)
# [
#   ('zoomzoom', {'home_team': 30, 'kyojin': 6, 'hanshin': 30}),
#   ('koshien', {'home_team': 20, 'kyojin': 18, 'hiroshima': 12}),
#   ('tokyo_dome', {'home_team': 10, 'hiroshima': 20, 'hanshin': 8})
# ]

reference

Recommended Posts

Sort dict in dict (dictionary in dictionary) with a specific key
Dictionary key error → Resolve with key in dicionary
Clone with a specific branch / tag in GitPython
dict in dict Makes a dict a dict
Note) Batch conversion of specific symbols contained in a character string with a dictionary
Stop an instance with a specific tag in Boto3
Create a dictionary in Python
A memo when checking whether the specified key exists in the defined dictionary with python
Value sort of dictionary with complicated structure (sort by key structure by deep value)
Get the value of a specific key up to the specified index in the dictionary list in Python
Count specific strings in a file
Save a specific variable in tensorflow.session
In the python dictionary, if a non-existent key is accessed, initialize it with an arbitrary value
Generate all files with a specific extension
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Draw a heart in Ruby with PyCall
Hash in Perl is a dictionary in Python
Use something other than a <br> string for the <br> dict key in Python
[Python] Get the files in a folder with Python
Draw a graph with Japanese labels in Jupyter
Delete data in a pattern with Redis Cluster
Start Django in a virtual environment with Pipenv
How to make a dictionary with a hierarchical structure.
Create a virtual environment with conda in Python
A memo about writing merge sort in Python
Build a Django environment with Vagrant in 5 minutes
Extract lines containing a specific "string" in Pandas
Work in a virtual environment with Python virtualenv.
Create a new page in confluence with Python
A story packed with absolute values in numpy.ndarray
Enter a specific value for variable in tensorflow
Configure a module with multiple files in Django
Sort list elements in a specified order in Python
How to count numbers in a specific range
Isn't there a default value in the dictionary?
Get a row containing a specific element in np.where
[Python] Manipulating elements in a list (array) [Sort]
[Python] Leave only the elements that start with a specific character string in the array
Python2 / numpy> Replace only a specific column in a file with column data from another file> numpy.c_
Train MNIST data with a neural network in PyTorch
I want to transition with a button in flask
Specific sample code for working with SQLite3 in Python
Receive dictionary data from a Python program in AppleScript
Playing with a user-local artificial intelligence API in Python
How to convert a class object to a dictionary with SQLAlchemy
Extract elements other than a specific index with Numpy
Make a simple Slackbot with interactive button in python
Drawing a tree structure with D3.js in Jupyter Notebook
Get the result in dict format with Python psycopg2
I want to work with a robot in python.
A addictive point in "Bayesian inference experience with Python"
[Python] How to sort dict in list and instance in list
Output a character string with line breaks in PyYAML
I can't manipulate iframes in a page with Selenium
Sort post data in reverse order with Django's ListView
Run a Python file with relative import in PyCharm
Create a fake Minecraft server in Python with Quarry
Apply conda's env to a specific directory with pyenv-vertualenv
Recursively get the Excel list in a specific folder with python and write it to Excel.
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.