![]() |
In Python, developers frequently seek tools to improve code readability and functionality. The `natsorted()` function stands out as a valuable tool for sorting objects with natural ordering. While the standard `sorted()` function struggles with human-readable strings containing numbers, `natsorted()` intelligently organizes such strings more intuitively. This article explores the syntax, an example of the `natsorted()` function, providing insights for Python developers dealing with naturally ordered data. Python natsorted() Function Syntax
What is natsorted() Function in Python?`natsorted()` is a Python function that simplifies the sorting of objects with natural ordering. It is particularly useful when dealing with strings that contain numeric components. Unlike the standard `sorted()` function, which may not handle such strings intuitively, `natsorted()` intelligently orders them in a more natural and human-readable manner. Developers often use this function to enhance code readability and functionality. How does natsort work?The typical lexicographic sorting algorithm treats strings as sequences of characters and compares them based on the ASCII or Unicode values of the characters. This works well for simple strings but can lead to unexpected results when sorting strings that contain numerical values. Example: String Sorting Let’s consider a list of fruit names and sort them using natsort: Python3
Output ['apple10', 'apple2', 'banana21', 'banana5', 'orange'] Without natsort, the lexicographic sorting results in ‘apple10’ coming before ‘apple2’ and ‘banana21’ coming before ‘banana5’, which may not be the desired order. Now, let’s use natsorted: Python3
Output ['apple2', 'apple10', 'banana5', 'banana21', 'orange'] With natsorted, the list is sorted based on the natural order of the embedded numbers, resulting in a more intuitive ordering of the fruit names. This showcases how natsort can be applied to various scenarios where strings contain embedded numerical values, providing a more human-friendly sorting order. More Python natsorted() Function ExamplesThere are various ways to natsorted() function in Python as a sorted way, The natsort library provides by implementing natural sorting algorithms. You can use the natsort library by installing it first: pip install natsort Now, let’s look at five examples of using natsort with a list of strings:
Python Sorting NumbersIn this example code sorts a list of version strings (`versions`) using natural ordering, resulting in `[‘1.2’, ‘1.9’, ‘1.10’, ‘1.21’]`, demonstrating intuitive sorting of numeric components within the versions. The `natsorted()` function intelligently handles the alphanumeric structure, producing a human-readable order. Python3
Output ['1.2', '1.9', '1.10', '1.21'] Time Complexity: O(n log n) Sorting Mixed Alphanumeric StringsIn this example the code sorts a list of mixed alphanumeric strings (`mixed_data`) using the `natsorted()` function, producing a naturally ordered result, and then prints the sorted list. Output: `[‘abc9’, ‘abc22’, ‘abc45’, ‘abc123’]`. Python3
Output : ['abc9', 'abc22', 'abc45', 'abc123'] Time Complexity: O(n log n) Python natsorted() Function Case-insensitive SortingIn this example the code sorts a list of strings (`case_insensitive_data`) in a case-insensitive manner using the `natsorted()` function with the `alg=natsorted.IC` parameter, and then prints the sorted result. Python3
Output ['Apple', 'banana', 'grape', 'Orange'] Time Complexity: O(n log n) |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |