Implementation of original sorting in Python

Normal sort

If you want to sort the List in python, you can easily sort it by using the sort () function as shown below.

>>> l = ['B', 'C', 'A']
>>> l.sort()
>>> print l
['A', 'B', 'C']

However, if you sort the file list containing dots such as .file, the files containing dots will be lined up first as shown below.

>>> files = ['file01', 'file02', '.file04', 'file03']
>>> files.sort()
>>> print files
['.file04', 'file01', 'file02', 'file03']

Original sort using lambda expression

For example, if you want to sort by the part that does not contain dots like the output of the ls command, you can do it by using a lambda expression as follows.

>>> files = ['file01', 'file02', '.file04', 'file03']
>>> sortKey = lambda f: f if not f.startswith('.') else f[1:]
>>> files.sort(key=sortKey)
>>> print files
['file01', 'file02', 'file03', '.file04']

The sortKey specifies how to generate a key for sorting. Specifically, the sort target is passed as f, and if there is a dot at the beginning off.startswith ('.'), The second character is cut out and used as the key.

Other applications

For example, you can sort the list by file size by doing the following:

>>> import os
>>> files = ['file01', 'file02', '.file04', 'file03']
>>> sortKey = lambda f: os.state(f).st_size
>>> files.sort(key=sortKey)


Recommended Posts

Implementation of original sorting in Python
Implementation of quicksort in Python
Sorting algorithm and implementation in Python
Implementation of life game in Python
RNN implementation in python
Basic sorting in Python
ValueObject implementation in Python
SVM implementation in python
Explanation of edit distance and implementation in Python
Equivalence of objects in Python
Techniques for sorting in Python
Python implementation of particle filters
Neural network implementation in python
Maximum likelihood estimation implementation of topic model in python
Overview of generalized linear models and implementation in Python
Variational Bayesian inference implementation of topic model in python
A reminder about the implementation of recommendations in Python
Maybe in a python (original title: Maybe in Python)
Pixel manipulation of images in Python
HMM parameter estimation implementation in python
Python implementation of self-organizing particle filters
Mixed normal distribution implementation in python
MySQL-automatic escape of parameters in python
Handling of JSON files in Python
Waveform display of audio in Python
Implementation of desktop notifications using Python
Python implementation of non-recursive Segment Tree
Implementation of Light CNN (Python Keras)
Law of large numbers in python
Implementation of Dijkstra's algorithm with python
Reversible scrambling of integers in Python
Check the behavior of destructor in Python
(Bad) practice of using this in Python
General Theory of Relativity in Python: Introduction
Output tree structure of files in Python
Comparison of Japanese conversion module in Python3
It's an implementation of ConnectionPool in redis.py
Summary of various for statements in Python
The result of installing python in Anaconda
Gang of Four (GoF) Patterns in Python
Bulk replacement of strings in Python arrays
Project Euler # 16 "Sum of Powers" in Python
Traffic Safety-kun: Recognition of traffic signs in Python
Non-logical operator usage of or in python
In search of the fastest FizzBuzz in Python
Practical example of Hexagonal Architecture in Python
Project Euler # 17 "Number of Characters" in Python
Double pendulum equation of motion in python
Get rid of DICOM images in Python
Non-recursive implementation of extended Euclidean algorithm (Python)
Python implementation of continuous hidden Markov model
Status of each Python processing system in 2020
Project Euler # 1 "Multiples of 3 and 5" in Python
Meaning of using DI framework in Python
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
Meta-analysis in Python
Unittest in python