Open In App

Python | Sort Numerical Records in String

Sometimes, while working with Python records we can have a problem that they may occur in name and number format in strings. These may be required to be sorted. This problem can occur in many domains in which data is involved. Let us discuss certain ways in which this task can be performed. 

Method #1 : Using join() + split() + sorted() + list comprehension 



The combination of above functions can be used to perform this task. In this, we perform the task of sort using sorted(), and task of extracting numbers using split(). We perform the task of rejoining sorted string using join(). 




# Python3 code to demonstrate working of
# Sort Numerical Records in String
# Using join() + split() + sorted() + list comprehension
 
# initializing string
test_str = "Akshat 15 Nikhil 20 Akash 10"
 
# printing original string
print("The original string is : " + test_str)
 
# Sort Numerical Records in String
# Using join() + split() + sorted() + list comprehension
temp1 = test_str.split()
temp2 = [temp1[idx: idx + 2] for idx in range(0, len(temp1), 2)]
temp3 = sorted(temp2, key=lambda ele: (int(ele[1]), ele[0]))
res = ' '.join([' '.join(ele) for ele in temp3])
 
# printing result
print("The string after sorting records : " + res)

Output : 

The original string is : Akshat 15 Nikhil 20 Akash 10
The string after sorting records : Akash 10 Akshat 15 Nikhil 20

Time complexity: O(n log n) where n is the number of records in the string.
Auxiliary space complexity: O(n) where n is the number of records in the string. 

Method #2: Using regex

This task can also be performed using regex. We perform the task of finding numbers using regex and rest of sorting and joining is performed as the above method. 




# Python3 code to demonstrate working of
# Sort Numerical Records in String
# Using regex
import re
 
# initializing string
test_str = "Akshat 15 Nikhil 20 Akash 10"
 
# printing original string
print("The original string is : " + test_str)
 
# Sort Numerical Records in String
# Using regex
temp1 = re.findall(r'([A-z]+) (\d+)', test_str)
temp2 = sorted(temp1, key=lambda ele: (int(ele[1]), ele[0]))
res = ' '.join(' '.join(ele) for ele in temp2)
 
# printing result
print("The string after sorting records : " + res)

Output
The original string is : Akshat 15 Nikhil 20 Akash 10
The string after sorting records : Akash 10 Akshat 15 Nikhil 20

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #3: Using dictionaries

Use dictionaries to store the name and corresponding numerical value as key-value pairs, and then sort the dictionary based on the numerical value. Finally, you can combine the sorted keys and values to form the sorted string.




# initializing string
test_str = "Akshat 15 Nikhil 20 Akash 10"
 
# printing original string
print("The original string is : " + test_str)
 
# Sort Numerical Records in String
# Using dictionaries
temp1 = test_str.split()
temp2 = {}
for i in range(0, len(temp1), 2):
    temp2[temp1[i]] = int(temp1[i+1])
temp3 = sorted(temp2.items(), key=lambda ele: ele[1])
res = ' '.join([k + ' ' + str(v) for k, v in temp3])
 
# printing result
print("The string after sorting records : " + res)

Output
The original string is : Akshat 15 Nikhil 20 Akash 10
The string after sorting records : Akash 10 Akshat 15 Nikhil 20

Time complexity: O(n log n), where n is the number of name-numerical pairs in the input string. 
Auxiliary space: O(n), where n is the number of name-numerical pairs in the input string. 

Method #4: Using list manipulation

program takes a string of records as input, sorts the numerical records based on the values, and returns the sorted string of records as output using list manipulation techniques such as splitting the string, mapping, sorting, and joining. The original string and the sorted string are printed as output.




# initializing string
test_str = "Akshat 15 Nikhil 20 Akash 10"
 
# printing original string
print("The original string is : " + test_str)
 
# Sort Numerical Records in String
# Using list manipulation
temp = test_str.split()
temp[1::2] = map(int, temp[1::2])
temp = sorted(zip(temp[1::2], temp[::2]))
res = ' '.join([x[1] + ' ' + str(x[0]) for x in temp])
 
# printing result
print("The string after sorting records : " + res)

Output
The original string is : Akshat 15 Nikhil 20 Akash 10
The string after sorting records : Akash 10 Akshat 15 Nikhil 20

The time complexity of the given code is O(n log n), where n is the number of records in the input string.
The space complexity of the code is O(n), where n is the number of records in the input string.

Method #6: Using lambda function and sorted()

Here is an approach that uses lambda function with sorted() to sort the numerical records in the given string.

Steps:

  1. Split the string into words using the split() method and store the result in a list.
  2. Convert the numerical values in the list to integers using map() and lambda function.
  3. Sort the list of tuples with the help of sorted() function and lambda function.
  4. Join the words in the list using join() method to get the desired output string.




# initializing string
test_str = "Akshat 15 Nikhil 20 Akash 10"
 
# printing original string
print("The original string is : " + test_str)
 
# Sort Numerical Records in String
# Using lambda function and sorted()
temp = test_str.split()
temp[1::2] = map(int, temp[1::2])
temp = sorted(zip(temp[::2], temp[1::2]), key=lambda x: x[1])
res = ' '.join([x[0] + ' ' + str(x[1]) for x in temp])
 
# printing result
print("The string after sorting records : " + res)

Output
The original string is : Akshat 15 Nikhil 20 Akash 10
The string after sorting records : Akash 10 Akshat 15 Nikhil 20

Time complexity: O(n log n)
Auxiliary space: O(n)


Article Tags :