Python – Numeric Sort in Mixed Pair String List

Sometimes, while working with data records, we can have a problem in which we receive data in custom format, and we need to perform a sort. We can receive both name of person and space-separated score and require to get the best to worst sorting. Lets discuss a way in which this particular problem can be solved.

Method #1 : Using split() + sort() + key function
The combination of above functionalities can be used to perform this task. In this, we perform a sort using external function in which we split the string and extract the numeric part.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Numeric Sort in Mixed Pair String List
# using split() + sort() + key function
  
# helper function 
def helper_func(ele):
        name, val = ele.split()
        return int(val)
  
# Initializing list
test_list = ["Manjeet 5", "Akshat 7", "Akash 6", "Nikhil 10"]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Numeric Sort in Mixed Pair String List
# using split() + sort() + key function
test_list.sort(key = helper_func, reverse = True)
      
# printing result 
print ("The reverse sorted numerics are : " + str(test_list))

chevron_right


Output :

The original list is : ['Manjeet 5', 'Akshat 7', 'Akash 6', 'Nikhil 10']
The reverse sorted numerics are : ['Nikhil 10', 'Akshat 7', 'Akash 6', 'Manjeet 5']

 

Method #2 : Using split() + lambda + sorted()
The combination of above methods can be used to perform this task. In this, we perform split on numbers as above. The difference is that it is one liner and use lambda function to perform.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate 
# Numeric Sort in Mixed Pair String List
# using split() + sorted() + lambda
  
# Initializing list
test_list = ["Manjeet 5", "Akshat 7", "Akash 6", "Nikhil 10"]
  
# printing original list
print("The original list is : " + str(test_list))
  
# Numeric Sort in Mixed Pair String List
# using split() + sorted() + lambda
res = sorted(test_list, reverse = True, key = lambda ele: int(ele.split()[1]))
  
# printing result 
print ("The reverse sorted numerics are : " + str(res))

chevron_right


Output :

The original list is : ['Manjeet 5', 'Akshat 7', 'Akash 6', 'Nikhil 10']
The reverse sorted numerics are : ['Nikhil 10', 'Akshat 7', 'Akash 6', 'Manjeet 5']



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nidhi_biet