Thursday, December 26, 2024
Google search engine
HomeLanguagesSort numeric strings in a list in Python

Sort numeric strings in a list in Python

Sorting list is an easy task and has been dealt with in many situations. With Machine Learning and Data Science emerging, sometimes we can get the data in the format of list of numbers but with string as data type. Generic Sort functions give erroneous result in that case, hence, several other methods have to employed to perform this particular task. Let’s discuss ways in which this is performed. 
Method #1 : Naive Method In the naive method requires the type conversion of all the elements into integers of the list iterated through a loop. After that generic sort function is employed to perform the task. 

Python3




# Python3 code to demonstrate
# numeric string sorting
# using naive method
 
# initializing list
test_list = [ '4', '6', '7', '2', '1']
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using naive method
# numeric string sorting
for i in range(0, len(test_list)) :
    test_list[i] = int(test_list[i])
test_list.sort()
 
# printing result
print ("The resultant sorted list  : " +  str(test_list))


Output:

The original list is : ['4', '6', '7', '2', '1']
The resultant sorted list  : [1, 2, 4, 6, 7]

Time Complexity: O(nlogn), where n is the length of the input list. This is because we’re sort() which has a time complexity of O(nlogn) in the worst case.
Auxiliary Space: O(n), as we’re using additional space other than the input list itself with the same size of input list.

  Method #2 : Using sort() using key The generic sort() can be used to perform this particular task, but has to be specified with the key as integer to convert it to integer while performing sort function internally. 

Python3




# Python3 code to demonstrate
# numeric string sorting
# using sort() + key
 
# initializing list
test_list = [ '4', '6', '7', '2', '1']
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using sort() + key
# numeric string sorting
test_list.sort(key = int)
 
# printing result
print ("The resultant sorted list  : " +  str(test_list))


Output:

The original list is : ['4', '6', '7', '2', '1']
The resultant sorted list  : ['1', '2', '4', '6', '7']

  Method #3 : Using sorted() + key This function has a similar internal working as the above function. The improvement that this function offers than the above function is that it doesn’t change the order of original list and just returns a view to display, hence useful at situations where order has to be maintained. 

Python3




# Python3 code to demonstrate
# numeric string sorting
# using sorted() + key
 
# initializing list
test_list = [ '4', '6', '7', '2', '1']
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using sorted() + key
# numeric string sorting
res = sorted(test_list, key = int)
 
# printing result
print ("The resultant sorted list  : " +  str(res))


Output:

The original list is : ['4', '6', '7', '2', '1']
The resultant sorted list  : ['1', '2', '4', '6', '7']

  Method #4 : Using sorted() + key with len () This function has a similar internal working as the above function, but in cases of large integer up to 100 times faster compared to using key = int. The improvement that this function offers than the above function is performance. This sort method can also be applied on sort (). 

Python3




# Python3 code to demonstrate
# numeric string sorting
# using sorted () + key with (len (x), x)
 
# initializing list
test_list = [ '4', '6', '7', '2', '1']
 
# printing original list
print ("The original list is : " + str (test_list))
 
# using sorted () + key with (len (x), x)
# numeric string sorting
res = sorted (test_list, key = lambda x: (len (x), x))
 
# printing result
print ("The resultant sorted list  : " +  str (res))


Output:

The original list is : ['4', '6', '7', '2', '1']
The resultant sorted list  : ['1', '2', '4', '6', '7']

The time complexity of this code is O(n log n) due to the use of the sorted() function.
The space complexity of this code is O(n), as we create a new list res that contains the sorted elements of the input list test_list. 

Method #5 : Using Decimal module:

One approach is using the Decimal module from the decimal package. The Decimal class can be used to represent decimal numbers in Python, and has a built-in compare method that can be used to compare two Decimal objects. You can use this approach to sort a list of numeric strings as follows:

Python3




from decimal import Decimal
 
# initializing list
test_list = ['4', '6', '7', '2', '1']
 
# printing original list
print("The original list is:", test_list)
 
# using the Decimal class to sort the list
test_list.sort(key=Decimal)
 
# printing result
print("The sorted list is:", test_list)
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list is: ['4', '6', '7', '2', '1']
The sorted list is: ['1', '2', '4', '6', '7']

The time complexity of the approach using the Decimal module to sort a list of numeric strings is O(nlogn), where n is the number of elements in the list. This is because the sort method uses a comparison-based sorting algorithm, such as quicksort or mergesort, which has a time complexity of O(nlogn) in the average and worst case.

The space complexity of this approach is O(n), as it involves creating a new Decimal object for each element in the list. 

RELATED ARTICLES

Most Popular

Recent Comments