Given a list of strings. The task is to sort the list by the numeric part of the string.
Examples:
Input : test_list = [“Gfg34”, “is67”, “be3st”, “f23or”, “ge9eks”]
Output : [‘be3st’, ‘ge9eks’, ‘f23or’, ‘Gfg34’, ‘is67’]
Explanation : 3 < 9 < 23 < 34 < 67, numbers extracted from strings.
Input : test_list = [“Gfg4”, “is67”, “be3st”, “f23or”, “ge9eks”]
Output : [‘be3st’, ‘Gfg4’, ‘ge9eks’, ‘f23or’, ‘is67’]
Explanation : 3 < 4 < 9 < 23 < 67, numbers extracted from strings.
Method #1 : Using sort() + re.findall()
In this, we perform the task of sorting using sort(), the explicit function is used to extract numbers using findall().
Python3
import re
def num_sort(test_string):
return list ( map ( int , re.findall(r '\d+' , test_string)))[ 0 ]
test_list = [ "Gfg34" , "is67" , "be3st" , "f23or" , "ge9eks" ]
print ( "The original list is : " + str (test_list))
test_list.sort(key = num_sort)
print ( "Strings after numerical Sort : " + str (test_list))
|
Output
The original list is : ['Gfg34', 'is67', 'be3st', 'f23or', 'ge9eks']
Strings after numerical Sort : ['be3st', 'ge9eks', 'f23or', 'Gfg34', 'is67']
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Method #2 : Using sort() + lambda + findall()
In this, we perform a similar function, but the difference being we use lambda function rather than an external function to perform the task of sorting.
Python3
import re
test_list = [ "Gfg34" , "is67" , "be3st" , "f23or" , "ge9eks" ]
print ( "The original list is : " + str (test_list))
test_list.sort(key = lambda test_string : list (
map ( int , re.findall(r '\d+' , test_string)))[ 0 ])
print ( "Strings after numerical Sort : " + str (test_list))
|
Output
The original list is : ['Gfg34', 'is67', 'be3st', 'f23or', 'ge9eks']
Strings after numerical Sort : ['be3st', 'ge9eks', 'f23or', 'Gfg34', 'is67']
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Method #3 : Using extend(),sort() and index() methods
Python3
test_list = [ "Gfg34" , "is67" , "be3st" , "f23or" , "ge9eks" ]
print ( "The original list is : " + str (test_list))
x = []
dig = "0123456789"
for i in test_list:
p = ""
for j in i:
if j in dig:
p + = j
x.append( int (p))
y = []
y.extend(x)
x.sort()
res = []
for i in x:
res.append(test_list[y.index(i)])
print ( "Strings after numerical Sort : " + str (res))
|
Output
The original list is : ['Gfg34', 'is67', 'be3st', 'f23or', 'ge9eks']
Strings after numerical Sort : ['be3st', 'ge9eks', 'f23or', 'Gfg34', 'is67']
Time Complexity : O(NlogN)
Auxiliary Space : O(N)
Method#4: Using list comprehension
Python3
import re
test_list = [ "Gfg34" , "is67" , "be3st" , "f23or" , "ge9eks" ]
print ( "The original list is : " + str (test_list))
substrings = [ int (x) for string in test_list for x in re.findall(r '\d+' , string)]
substrings.sort()
res = [string for x in substrings for string in test_list if int (re.findall(r '\d+' , string)[ 0 ]) = = x]
print ( "Strings after numerical Sort : " + str (res))
|
Output
The original list is : ['Gfg34', 'is67', 'be3st', 'f23or', 'ge9eks']
Strings after numerical Sort : ['be3st', 'ge9eks', 'f23or', 'Gfg34', 'is67']
Time Complexity : O(NlogN)
Auxiliary Space : O(N)
Method #5: Using isdigit() method
- Initialize the input list of strings test_list.
- Use the sorted() function to sort the list based on a key function. The key function is defined using a lambda function that takes each string x in the list and performs the following steps:
a. Use filter() to extract only the digits from the string.
b. Use join() to concatenate the digits into a single string.
c. Use int() to convert the resulting string of digits to an integer.
d. Return the integer as the sorting key.
- Assign the sorted list to the variable sorted_list.
- Print the sorted list using the print() function.
Python3
test_list = [ "Gfg34" , "is67" , "be3st" , "f23or" , "ge9eks" ]
print ( "The original list is:" ,test_list)
sorted_list = sorted (test_list, key = lambda x: int (''.join( filter ( str .isdigit, x))))
print ( "List after numeric sort is:" ,sorted_list)
|
Output
The original list is: ['Gfg34', 'is67', 'be3st', 'f23or', 'ge9eks']
List after numeric sort is: ['be3st', 'ge9eks', 'f23or', 'Gfg34', 'is67']
Time Complexity : O(NlogN)
Auxiliary Space : O(N)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 May, 2023
Like Article
Save Article