Python | Sort given list of strings by part of string
Last Updated :
04 May, 2023
Given a list of strings, the task is to sort the list by part of the string which is separated by some character. In this scenario, we are considering the string to be separated by space, which means it has to be sorted by second part of each string. Given below are a few methods to solve the given task.
Method #1: Using sort
Python3
ini_list = [ "GeeksForGeeks abc" , "manjeet xab" , "akshat bac" ]
print ( "initial list" , str (ini_list))
ini_list.sort(key = lambda x: x.split()[ 1 ])
print ( "result" , str (ini_list))
|
Output:
initial list ['GeeksForGeeks abc', 'manjeet xab', 'akshat bac']
result ['GeeksForGeeks abc', 'akshat bac', 'manjeet xab']
Time complexity: O(nlogn) – Sorting takes O(nlogn) time and the lambda function takes O(n) time in the worst case.
Auxiliary space: O(1) – No extra space is used. Sorting is done in-place.
Method #2: Using sorted
Python3
ini_list = [ "GeeksForGeeks abc" , "manjeet xab" , "akshat bac" ]
print ( "initial list" , str (ini_list))
res = sorted (ini_list, key = lambda x: x.split()[ 1 ])
print ( "result" , res)
|
Output:
initial list ['GeeksForGeeks abc', 'manjeet xab', 'akshat bac']
result ['GeeksForGeeks abc', 'akshat bac', 'manjeet xab']
Time Complexity: O(nlogn)
Auxiliary Space O(n)
Method #3: Using itemgetter
To split the string and sort the list by the second part of the string using the itemgetter function, you can do the following:
This will split each string into a list of two elements using the split function, and then pass the resulting list to the itemgetter function, which will retrieve the second element (the part of the string after the first space). The lambda function will then be used as the sorting key, so that the list is sorted based on the second element of each list.
Python3
from operator import itemgetter
ini_list = [ "GeeksForGeeks abc" , "manjeet xab" , "akshat bac" ]
sorted_list = sorted (ini_list, key = lambda x: itemgetter( 1 )(x.split()))
print (sorted_list)
|
Output
['GeeksForGeeks abc', 'akshat bac', 'manjeet xab']
Time complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using regular expression
This approach uses the re.findall() function from the re module to extract the last word of each string. It then sorts the list using the extracted words as the key.
Algorithm:
- Define the input list of strings lst and a regular expression regex.
- Use the sorted() function to sort the list lst based on the last word of each string.
- Define a lambda function that extracts the last word of a string using the re.findall() function and returns it as the key for sorting.
- Print the sorted list.
Python3
import re
lst = [ 'GeeksForGeeks abc' , 'manjeet xab' , 'akshat bac' ]
regex = r '\w+$'
result = sorted (lst, key = lambda x: re.findall(regex, x)[ 0 ])
print (result)
|
Output
['GeeksForGeeks abc', 'akshat bac', 'manjeet xab']
Time complexity: O(nlogn), where n is the length of the input list. This is because the sorted() function uses a stable sorting algorithm with a time complexity of O(nlogn) on average. The re.findall() function has a time complexity of O(n) for a string of length n.
Auxiliary Space: O(n), as it creates a new sorted list with the same length as the input list. It also creates a new list of extracted words, but the length of this list is proportional to the length of the input list. Therefore, the space complexity is also linear.
Method 5: Using counter method
The program takes a list of strings, extracts a substring from each string, groups the strings based on the substrings using a defaultdict, sorts the substrings in lexicographic order, and concatenates the corresponding lists of strings to produce the final sorted list of strings.
Steps:
- Create an empty defaultdict object, d.
- For each string s in the input list lst, extract the second substring from s and store it in substr.
- Append s to the list associated with substr in the defaultdict d.
- Sort the keys of d in lexicographic order and store them in a list substrings.
- Concatenate the lists of strings associated with each key in substrings to produce the final sorted list sorted_lst.
Python3
from collections import defaultdict
lst = [ 'GeeksForGeeks abc' , 'manjeet xab' , 'akshat bac' ]
d = defaultdict( list )
for s in lst:
substr = s.split()[ 1 ]
d[substr].append(s)
sorted_lst = [s for substr in sorted (d.keys()) for s in d[substr]]
print (sorted_lst)
|
Output
['GeeksForGeeks abc', 'akshat bac', 'manjeet xab']
Time complexity: O(N log(N)), where n is the number of strings in the input list. This is because sorting the keys of the defaultdict takes O(k log k) time, where k is the number of unique substrings, and k is at most n.
Auxiliary Space: O(n), where n is the number of strings in the input list. This is because the program uses a defaultdict to store the strings, and the size of the defaultdict is proportional to the number of strings in the input list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...