Home >Backend Development >Python Tutorial >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']
Sorting this list with sorted() produces the following:
['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9']
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
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())
This will produce the desired natural sorting order:
['elm0', 'elm1', 'Elm2', 'elm9', 'elm10', 'Elm11', 'Elm12', 'elm13']
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)
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!