Home > Backend Development > Python Tutorial > How to Sort String Numbers Numerically in Python?

How to Sort String Numbers Numerically in Python?

Susan Sarandon
Release: 2024-12-08 00:07:11
Original
145 people have browsed it

How to Sort String Numbers Numerically in Python?

Sorting String Numbers Numerically

When dealing with a list of strings that represent numerical values, sorting them based on their actual数値 may seem like a straightforward task. However, the default sort() function in Python can provide unexpected results.

The issue arises because the sort() function compares the strings lexicographically, meaning it sorts them alphabetically instead of numerically. To resolve this, it's crucial to convert the strings to actual integers before performing the sort.

Consider the following Python code:

list1 = ["1", "10", "3", "22", "23", "4", "2", "200"]
for item in list1:
    item = int(item)

list1.sort()
Copy after login

In this code, we first convert each string in the list to an integer using the int() function. However, we then neglect to use the converted integer values in the sort. As a result, the function compares the original strings and produces the following output:

['1', '10', '2', '200', '22', '23', '3', '4']
Copy after login

To sort the list numerically, we need to incorporate the converted integers into the sort, as shown below:

list1 = [int(x) for x in list1]
list1.sort()
Copy after login

This time, the function will correctly compare the integer values and produce the desired output:

['1', '2', '3', '4', '10', '22', '23', '200']
Copy after login
Copy after login

Alternatively, if you need to preserve the strings instead of converting them to integers, you can use a key function while sorting. The sort() function allows you to specify a key function that is applied to each element before it is compared. This enables you to customize the comparison criteria.

For instance, to sort the list of strings numerically using a key function, you can use the following code:

list1 = ["1", "10", "3", "22", "23", "4", "2", "200"]
list1.sort(key=int)
Copy after login

In this case, the key function int() is applied to each string before comparison. The function converts the strings to integers, and the sort() function compares the resulting integer values, producing the same desired output:

['1', '2', '3', '4', '10', '22', '23', '200']
Copy after login
Copy after login

By understanding how the sort() function works in Python and utilizing key functions when necessary, you can effectively sort lists of string numbers numerically, regardless of their original format.

The above is the detailed content of How to Sort String Numbers Numerically 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