[Python] Sort the list of pathlib.Path in natural sort

Overview

The natsorted () function cannot be applied to lists with elements in the pathlib.Path format. Therefore, I made my own function for sorting in natural order.

sorted () and natsorted () functions

The sorted () function is a function that sorts the elements of a list in lexicographical order. For example, when there is such a list

strs = ["dir/10", "dir/1", "dir/3" , "dir/24"]

If you write like this,

for s in sorted(strs):
    print(s)

It becomes like this.

dir/1
dir/10
dir/24
dir/3

It's in dictionary order, so it's reasonable, but it's a bit unpleasant for humans.

On the other hand, the natsorted () function is a function that sorts the elements of a list in natural order (a module is required). If you write like this for the same list,

from natsort import natsorted

for s in natsorted(strs):
    print(s)

It becomes like this.

dir/1
dir/3
dir/10
dir/24

This one fits nicely. The best natsorted () function!

What happens if Path is an element

For example, in the directory structure below,

dir
  ├ 1
  ├ 3
  ├ 10
  └ 24

In this way, you can create a list in pathlib.Path format. If you look up the pathlib module, you can find a lot of explanations, so I will omit it.

import pathlib

paths = [p for p in pathlib.Path("./dir/").iterdir() if p.is_dir()]

Then, if you sort this in natural order,

from natsort import natsorted

for p in natsorted(paths):
    print(p)

It becomes like this.

dir\1
dir\10
dir\24
dir\3

** It's in dictionary order! ** ** Well, I'm sure it doesn't support the Path format. So if you read it in dictionary order and it's just that. Does it mean that the element must be str or int (I don't understand this area well. I want information).

solution

I made my own function like this. The sorted () function makes use of the fact that key can be specified as an argument.

def paths_sorted(paths):
    return sorted(paths, key = lambda x: int(x.name))

If you use this like this,

for p in paths_sorted(paths):
    print(p)

It becomes like this.

dir\1
dir\3
dir\10
dir\24

The desired result was obtained.

Other

In my case, the directory name was a numerical value, so I converted the directory name to an int and used it as a key. If you want to include a string like no1, no2 ..., you can use natsorted () as str (although you need a module). The same method should be applicable to files instead of directories.

Recommended Posts

[Python] Sort the list of pathlib.Path in natural sort
Make a copy of the list in Python
[python] Get the list of classes defined in the module
[Python] Outputs all combinations of elements in the list
About the basics list of Python basics
Sort and output the elements in the list as elements and multiples in Python.
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Bubble Sort)
Get the number of specific elements in a python list
Implemented the algorithm of "Algorithm Picture Book" in Python3 (selection sort)
Sort tuple list in Python by specifying the ascending / descending order of multiple keys
Check the behavior of destructor in Python
Display a list of alphabets in Python 3
OR the List in Python (zip function)
The result of installing python in Anaconda
The basics of running NoxPlayer in Python
Summary of built-in methods in Python list
In search of the fastest FizzBuzz in Python
Get the EDINET code list in Python
[python] Get the rank of the values in List in ascending / descending order
Sorted list in Python
List of python modules
Output the number of CPU cores in Python
[python] Check the elements of the list all, any
Bubble sort in Python
Get the caller of a function in Python
Match the distribution of each group in Python
Filter List in Python
View the result of geometry processing in Python
the zen of Python
[Introduction to Python] How to sort the contents of a list efficiently with list sort
Find the divisor of the value entered in python
[Memo] Python3 list sort
I want to sort a list in the order of other lists
List find in Python
Find the solution of the nth-order equation in python
Receive a list of the results of parallel processing in Python with starmap
The story of reading HSPICE data in Python
[Note] About the role of underscore "_" in Python
About the behavior of Model.get_or_create () of peewee in Python
Solving the equation of motion in Python (odeint)
Custom sort in Python3
Output in the form of a python array
Search by the value of the instance in the list
Sort list elements in a specified order in Python
Natural order in python
[Python] Manipulating elements in a list (array) [Sort]
How to pass the execution result of a shell command in a list in Python
plot the coordinates of the processing (python) list and specify the number of times in draw ()
How to get a list of files in the same directory with python
Experience the good calculation efficiency of vectorization in Python
Sort in Python. Next, let's think about the algorithm.
How to get the number of digits in Python
Summary of Python sort (list, dictionary type, Series, DataFrame)
[Python] Manipulation of elements in list (array) [Add / Delete]
The story of FileNotFound in Python open () mode ='w'
Learn the design pattern "Chain of Responsibility" in Python
Implement the solution of Riccati algebraic equations in Python
Get the size (number of elements) of UnionFind in Python
Not being aware of the contents of the data in python
List of Python code used in big data analysis
[Python] Get the list of ExifTags names of Pillow library