Home > Backend Development > Python Tutorial > How Can I Achieve Natural String Sorting in Python?

How Can I Achieve Natural String Sorting in Python?

Patricia Arquette
Release: 2024-12-26 11:17:09
Original
443 people have browsed it

How Can I Achieve Natural String Sorting in Python?

Natural String Sorting in Python

In Python, sorting a list of strings alphabetically using the default sorted() function may not yield the desired "natural" order. This is because sorted() treats strings as individual characters, resulting in an unnatural ordering.

For example, consider the following list of strings:

['elm0', 'elm1', 'Elm2', 'elm9', 'elm10', 'Elm11', 'Elm12', 'elm13']
Copy after login
Copy after login

Sorting this list with sorted() produces the following:

['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9']
Copy after login

This is not the natural sorting order, as numbers following letters are alphabetized as opposed to treated as part of the number.

To achieve natural string sorting, you can utilize the natsort library. Natsort provides functions that sort strings in a manner that is more intuitive, preserving the natural ordering of numbers.

You can install natsort using pip:

pip install natsort
Copy after login

To perform natural sorting on the given list, use the natsorted() function:

from natsort import natsorted

x = ['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9']
natsorted(x, key=lambda y: y.lower())
Copy after login

This will produce the desired natural sorting order:

['elm0', 'elm1', 'Elm2', 'elm9', 'elm10', 'Elm11', 'Elm12', 'elm13']
Copy after login
Copy after login

The natsort_keygen() function can also be used to create a sorting key:

from natsort import natsort_keygen

natsort_key = natsort_keygen(key=lambda y: y.lower())
list.sort(key=natsort_key)
Copy after login

This will sort the list in the same manner as natsorted().

In addition to natsorted() and natsort_keygen(), natsort also provides os_sorted() and os_sort_keygen() functions. These functions allow you to sort lists according to the sorting behavior of your operating system's file explorer, which may be different from the default behavior of natsort.

The above is the detailed content of How Can I Achieve Natural String Sorting in Python?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template