Techniques for sorting in Python

Introduction

Note that you may forget how to sort when you are competing in Python3

Below, since we are running in interpreted mode, think of what has a >>>> at the beginning of the line as input.

Difference between list.sort () and sorted (list)

list.sort () sorts the original list. The return value is None

>>>> list = [2, 3, 5, 1, 4]
>>>> list.sort()
>>>> list
[1, 2, 3, 4, 5]

sorted (list) returns the result of sorting without changing the original list

>>>> list = [2, 3, 5, 1, 4]
>>>> sorted(list)
[1, 2, 3, 4, 5]
>>>> list
[2, 3, 5, 1, 4]

When you want to sort in reverse order

Below, both list.sort () and sorted (list) use the same options, so only sorted (list) is handled.

Optionally specify reverse = True

>>>> list = [2, 3, 5, 1, 4]
>>>> sorted(list)
[1, 2, 3, 4, 5]
>>>> sorted(list, reverse=True)
[5, 4, 3, 2, 1]

When you want to limit the columns used for sorting

When sorting normally, it sorts in order from the left column. Image like dictionary order

>>>> people = [('Bob', 12), ('Alice', 10), ('Chris', 8), ('Chris', 7)]
>>>> sorted(people)
[('Alice', 10), ('Bob', 12), ('Chris', 7), ('Chris', 8)]

When you want to use only the second column for sorting

>>>> sorted(people, key=lambda x:x[1])
[('Chris', 7), ('Chris', 8), ('Alice', 10), ('Bob', 12)]

When you want to use the 1st and 2nd columns for sorting

>>>> sorted(people, key=lambda x:(x[0], x[1]))
[('Alice', 10), ('Bob', 12), ('Chris', 7), ('Chris', 8)]

in conclusion

If you use ʻitemgetter, ʻattrgetter, you don't have to write a lambda expression, but I decided not to use it because I will learn more.

If you want to use it, please check it from the reference link.

reference

https://docs.python.org/3/howto/sorting.html

Recommended Posts

Techniques for sorting in Python
Basic sorting in Python
About "for _ in range ():" in python
Check for memory leaks in Python
Check for external commands in python
Sorting algorithm and implementation in Python
Python course for data science_useful techniques
Implementation of original sorting in Python
Run unittests in Python (for beginners)
2016-10-30 else for Python3> for:
Quadtree in Python --2
Python in optimization
python [for myself]
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Notes on nfc.ContactlessFrontend () for nfcpy in python
Inject is recommended for DDD in Python
Tips for dealing with binaries in Python
[python] Frequently used techniques in machine learning
Type annotations for Python2 in stub files!
Epoch in Python
Discord in Python
Sudoku in Python
nCr in python
N-Gram in Python
Template for writing batch scripts in python
Programming in python
Detailed Python techniques required for data shaping (1)
Process multiple lists with for in Python
Plink in Python
Constant in python
MongoDB for the first time in Python
Get a token for conoha in python
Lifegame in Python.
FizzBuzz in Python
Sample for handling eml files in Python
Sqlite in python
StepAIC in Python
AtCoder cheat sheet in python (for myself)
N-gram in python
Summary of useful techniques for Python Scrapy
I searched for prime numbers in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Notes for using python (pydev) in eclipse
Reflection in Python
Tips for making small tools in python
Constant in python
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7
nCr in Python.
format in python
Scons in Python3
Detailed Python techniques required for data shaping (2)
Puyo Puyo in python
python in virtualenv
PPAP in Python