Python program to sort strings by substring range
Last Updated :
22 Apr, 2023
Given a String List, sort by a range of substrings.
Input : test_list = [“geeksforgeeks”, “best”, “geeks”, “preparation”, “interview”], i, j = 1, 3
Output : [‘geeksforgeeks’, ‘geeks’, ‘best’, ‘interview’, ‘preparation’]
Explanation : “eek” < “eek” < “est” < “nte” < “rep”.
Input : test_list = [“apple”, “orange”, “banana”], i, j = 2, 4
Output : [‘orange’, ‘banana’, ‘apple’]
Explanation : “ang” < “nan” < “ple”.
Method #1 : Using sort() + slicing
In this, we perform the task of sorting using sort() and the task of extracting a range is done using slicing.
Python3
def get_substr(test_str):
return test_str[i : j]
test_list = [ "geeksforgeeks" , "best" , "geeks" , "preparation" ]
print ( "The original list is : " + str (test_list))
i, j = 1 , 3
test_list.sort(key = get_substr)
print ( "Strings after sorting : " + str (test_list))
|
Output
The original list is : ['geeksforgeeks', 'best', 'geeks', 'preparation']
Strings after sorting : ['geeksforgeeks', 'geeks', 'best', 'preparation']
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Method #2 : Using lambda function + sort() + slicing
In this, we perform slicing using lambda function rather than calling external function.
Python3
test_list = [ "geeksforgeeks" , "best" , "geeks" , "preparation" ]
print ( "The original list is : " + str (test_list))
i, j = 1 , 3
test_list.sort(key = lambda test_str : test_str[i : j])
print ( "Strings after sorting : " + str (test_list))
|
Output
The original list is : ['geeksforgeeks', 'best', 'geeks', 'preparation']
Strings after sorting : ['geeksforgeeks', 'geeks', 'best', 'preparation']
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Method #3: Using the functools.cmp_to_key() function and sorted()
Step-by-step algorithm:
- Import the cmp_to_key function from the functools module.
- Create a list of strings test_list.
- Initialize i and j to 1 and 3 respectively.
- Define a comparison function cmp that takes two strings a and b as input and compares the substring of a and b from index i to index j. If the substring of a is less than the substring of b, return -1. If the substring of a is greater than the substring of b, return 1. Otherwise, return 0.
- Sort the list test_list using the sorted() function and the key argument set to cmp_to_key(cmp).
- Print the sorted list.
Python3
from functools import cmp_to_key
test_list = [ "geeksforgeeks" , "best" , "geeks" , "preparation" ]
i, j = 1 , 3
print ( "The original list is : " + str (test_list))
def cmp (a, b):
if a[i:j] < b[i:j]:
return - 1
elif a[i:j] > b[i:j]:
return 1
else :
return 0
sorted_list = sorted (test_list, key = cmp_to_key( cmp ))
print (sorted_list)
|
Output
The original list is : ['geeksforgeeks', 'best', 'geeks', 'preparation']
['geeksforgeeks', 'geeks', 'best', 'preparation']
Time complexity: O(n log n), where n is the length of the list test_list. This is because sorting using the sorted() function takes O(n log n) time.
Auxiliary Space: O(n), where n is the length of the list test_list. This is because we are creating a new list to store the sorted version of the input list.
Method #4:Using itemgetter() from operator module
- Importing the operator module to use itemgetter() function for sorting by substring range.
- Initializing a list test_list containing strings.
- Printing the original list.
- Initializing the range (start, end) for the substring.
- Sorting the list using the sorted() function and itemgetter() function from the operator module.
- The key argument takes operator.itemgetter(i, j) as a parameter. It sorts the list based on the substring from index i to index j.
- Printing the sorted list.
Python3
import operator
test_list = [ "geeksforgeeks" , "best" , "geeks" , "preparation" ]
print ( "The original list is : " + str (test_list))
i, j = 1 , 3
test_list = sorted (test_list, key = operator.itemgetter(i, j))
print ( "Strings after sorting : " + str (test_list))
|
Output
The original list is : ['geeksforgeeks', 'best', 'geeks', 'preparation']
Strings after sorting : ['geeksforgeeks', 'geeks', 'best', 'preparation']
Time Complexity: O(n log n), where n is the length of the list test_list.
Auxiliary Space: O(n), where n is the length of the list test_list.
Method #6: Using list comprehension and sorted()
steps :
We initialize a list test_list with some strings.
We print the original list test_list.
We initialize two variables i and j to represent the start and end indices of the substring range we want to sort on.
We sort the list test_list using the sorted() function and a lambda function as the key. The lambda function extracts the substring range of each string in the list using the variables i and j. The sorted() function returns a new sorted list.
We update the original list test_list with the sorted list.
We print the updated list test_list.
Python3
test_list = [ "geeksforgeeks" , "best" , "geeks" , "preparation" ]
print ( "The original list is : " + str (test_list))
i, j = 1 , 3
test_list = sorted (test_list, key = lambda x: x[i:j + 1 ])
print ( "Strings after sorting : " + str (test_list))
|
Output
The original list is : ['geeksforgeeks', 'best', 'geeks', 'preparation']
Strings after sorting : ['geeksforgeeks', 'geeks', 'best', 'preparation']
Time complexity: O(n log n), where n is the length of the list.
Auxiliary space: O(n), where n is the length of the list.
Share your thoughts in the comments
Please Login to comment...