[python] How to sort by the Nth Mth element of a multidimensional array

Introduction

Today's theme is what I learned from this problem.

ABC 128  B-Guidebook

I learned about the applied technique of array sorting. Did beginners wonder, "What is the order of sorting multidimensional arrays (lists in a list)?" This time I will unravel it!

Way of thinking

The problem this time is to keep the "city name", "points", and "order" given by the input in a multidimensional array. Since it is necessary to output in ascending order of "city name" in dictionary order, and in descending order of "score" among them, if you output "order" of the list in ascending order in the first and second in descending order of the two-dimensional array It's good. The point is to specify the element you want to sort by lambda expression in the optional argument key. If there are multiple sort keys and the ascending / descending order is different for each, there is a technique to add a minus and invert as shown in the example below. It seems that you can use it if you remember.

image.png

Recommended Posts

[python] How to sort by the Nth Mth element of a multidimensional array
How to sort by specifying a column in the Python Numpy array.
How to determine the existence of a selenium element in Python
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to identify the element with the smallest number of characters in a Python list?
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
[Python] How to use the for statement. A method of extracting by specifying a range or conditions.
I made a function to see the movement of a two-dimensional array (Python)
How to calculate the volatility of a brand
How to erase the characters output by Python
Output in the form of a python array
Speed: Add element to end of Python array
[Python] How to sort instances by instance variables
How to pass the execution result of a shell command in a list in Python
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
Get the last element of the array by splitting the string in Python and PHP
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to retrieve the nth largest value in Python
How to get the number of digits in Python
How to write a list / dictionary type of Python3
How to save a table scraped by python to csv
[Python] Summary of how to specify the color of the figure
Python Note: The mystery of assigning a variable to a variable
How to execute a schedule by specifying the Python time zone and execution frequency
[Ruby] How to replace only a part of the string matched by the regular expression?
How to check in Python if one of the elements of a list is in another list
How to count the number of occurrences of each element in the list in Python with weight
How to find the first element that matches your criteria in a Python list
Add a function to tell the weather of today to slack bot (made by python)
[Ubuntu] How to delete the entire contents of a directory
How to switch the configuration file to be read by Python
How to slice a block multiple array from a multiple array in Python
How to test the attributes added by add_request_method of pyramid
How to convert an array to a dictionary with Python [Application]
How to shuffle a part of a Python list (at random.shuffle)
How to use the __call__ method in a Python class
How to transpose a 2D array using only python [Note]
Find the index of the maximum value (minimum value) of a multidimensional array
How to develop in a virtual environment of Python [Memo]
How to get the last (last) value in a list in Python
How to get a list of built-in exceptions in python
Debug by attaching to the Python process of the SSH destination
How to find the scaling factor of a biorthogonal wavelet
How to override a user-defined method generated by python swig
How to get a quadratic array of squares in a spiral!
How to connect the contents of a list into a string
Extract the value closest to a value from a Python list element
How to quickly count the frequency of appearance of characters from a character string in Python?
How to check if the contents of the dictionary are the same in Python by hash value
Create a 2D array by adding a row to the end of an empty array with numpy
How to plot a lot of legends by changing the color of the graph continuously with matplotlib
How to pass the execution result of a shell command in a list in Python (non-blocking version)
How to write a Python class
How to get the Python version
[Python] How to import the library
[Python] How to swap array values
[Python] How to force a method of a subclass to do something specific
How to change the log level of Azure SDK for Python
How to know the internal structure of an object in Python