[PYTHON] I want to sort a list in the order of other lists

What do you want to do

--When there are multiple related lists, I want to sort the other lists according to the sort order of one list. --Key specification using sorted lambda is often explained in the example of dictionary type data, but note that there are many related data that are not stored in dictionary type.

I want to sort people's names by age

If no one has the same name

When there are two arrays like the one below, I want to replace the names according to the sort order of ages.

names = ["Alice", "Bob", "Charlie"]
ages = [10, 30, 20]

It works, but

people = {name: age for name, age in zip(names, ages)}
people = sorted(people.items(), key=lambda x: x[1])
names_sorted = [name for name, _ in people]  # ['Alice', 'Charlie', 'Bob']

This method doesn't work if there are people with the same name.

If there is a person with the same name

names = ["Alice", "Bob", "Charlie", "Charlie"]
ages = [10, 30, 20, 5]

When using a dictionary, the second Charlie overwrites the first one.

people = {name: age for name, age in zip(names, ages)}
people = sorted(people.items(), key=lambda x: x[1])
names_sorted = [name for name, _ in people]  # ['Alice', 'Charlie', 'Bob']

If you try it without making it a dictionary, it will work.

people = [[name, age] for name, age in zip(names, ages)]
people = sorted(people, key=lambda x: x[1])
names_sorted = [name for name, _ in people]  # ['Charlie', 'Alice', 'Charlie', 'Bob']

Recommended Posts

I want to sort a list in the order of other lists
I want to see a list of WebDAV files in the Requests module
I want to set a life cycle in the task definition of ECS
I want to make the Dictionary type in the List unique
I want to print in a comprehension
I want to know the population of each country in the world.
[Python] Sort the list of pathlib.Path in natural sort
I want to create a window in Python
Make a copy of the list in Python
[Introduction to Python] How to sort the contents of a list efficiently with list sort
I want to store DB information in list
[Linux] Command to get a list of commands executed in the past
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
I want to color a part of an Excel string in Python
I want to leave an arbitrary command in the command history of Shell
Sort list elements in a specified order in Python
I made a program to check the size of a file in Python
I tried to display the altitude value of DTM in a graph
I want to customize the appearance of zabbix
Python: I want to measure the processing time of a function neatly
I want to display the progress in Python!
I want to use complicated four arithmetic operations in the IF statement of the Django template! → Use a custom template
How to pass the execution result of a shell command in a list in Python
The story of IPv6 address that I want to keep at a minimum
I want to use Python in the environment of pyenv + pipenv on Windows 10
I made an appdo command to execute a command in the context of the app
I want to add silence to the beginning of a wav file for 1 second
I want to store the result of% time, %% time, etc. in an object (variable)
How to get a list of files in the same directory with python
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to write in Python! (2) Let's write a test
I want to randomly sample a file in Python
I want to fully understand the basics of Bokeh
I want to work with a robot in python.
I want to install a package of Php Redis
[Python] I want to make a nested list a tuple
I want to write in Python! (3) Utilize the mock
[Python] How to output the list values in order
I want to use the R dataset in python
I want to increase the security of ssh connections
How to identify the element with the smallest number of characters in a Python list?
The story of Linux that I want to teach myself half a year ago
I want to take a screenshot of the site on Docker using any font
I want to use a network defined by myself in PPO2 of Stable Baselines
How to check in Python if one of the elements of a list is in another list
I want to do something like sort uniq in Python
I want to start a lot of processes from python
I want to use only the normalization process of SudachiPy
NikuGan ~ I want to see a lot of delicious meat! !!
I want to get the operation information of yahoo route
Put the lists together in pandas to make a DataFrame
Sort the string array in order of length & Japanese syllabary
I want to judge the authenticity of the elements of numpy array
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
I want to know the features of Python and pip
Keras I want to get the output of any layer !!
I want to align the significant figures in the Numpy array
I want to know the legend of the IT technology world