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
# Python code to demonstrate to sort list # containing string by part of string # Initialising list ini_list = [ "GeeksForGeeks abc" , "manjeet xab" , "akshat bac" ] # Printing initial list print ( "initial list" , str (ini_list)) # Sorting list ini_list.sort(key = lambda x: x.split()[ 1 ]) # Printing result print ( "result" , str (ini_list)) |
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
# Python code to demonstrate to sort list # containing string by part of string # Initialising list ini_list = [ "GeeksForGeeks abc" , "manjeet xab" , "akshat bac" ] # printing initial list print ( "initial list" , str (ini_list)) # code to sort list res = sorted (ini_list, key = lambda x: x.split()[ 1 ]) # printing result print ( "result" , res) |
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 # Initializing list ini_list = [ "GeeksForGeeks abc" , "manjeet xab" , "akshat bac" ] # Sorting the list sorted_list = sorted (ini_list, key = lambda x: itemgetter( 1 )(x.split())) # Printing the sorted list print (sorted_list) # This code is contributed by Edula Vinay Kumar Reddy |
['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+$' # matches the last word of the string result = sorted (lst, key = lambda x: re.findall(regex, x)[ 0 ]) print (result) |
['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 # input list 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) |
['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.