Python – Sort words separated by Delimiter
Last Updated :
23 Mar, 2023
Given string of words separated by some delimiter. The task is to sort all the words given in the string
Input : test_str = 'gfg:is:best:for:geeks', delim = "*"
Output : best*for*geeks*gfg*is
Explanation : Words sorted after separated by delim.
Input : test_str = 'gfg:is:best', delim = "*"
Output : best*gfg*is
Explanation : Words sorted after separated by delim.
Method: Using sorted() + join() + split()
The combination of the above functions can be used to solve this problem. In this, we segregate all the words by a particular delimiter using split(), and convert to list, then perform the sort of words, then reconvert to string attaching by the same delimiter.
Python3
test_str = 'gfg:is:best:for:geeks'
print ( "The original string is : " + str (test_str))
delim = ":"
res = delim.join( sorted (test_str.split( ':' )))
print ( "The sorted words : " + str (res))
|
Output
The original string is : gfg:is:best:for:geeks
The sorted words : best:for:geeks:gfg:is
Time Complexity: O(n)
Space Complexity: O(n)
Method #2: Using sort() function
Python3
test_str = 'gfg:is:best:for:geeks'
print ( "The original string is : " + str (test_str))
delim = ":"
test_list = test_str.split(delim)
test_list.sort()
res = delim.join(test_list)
print ( "The sorted words : " + str (res))
|
Output
The original string is : gfg:is:best:for:geeks
The sorted words : best:for:geeks:gfg:is
Time complexity: O(n log n).
Auxiliary space: O(n).
Method 3: Using lambda function with sorted()
This method we sorts the words in a case-insensitive manner. The lambda function takes each word in the list and returns its lowercase version, which is then used for sorting.
Python3
test_str = 'gfg:is:best:for:geeks'
print ( "The original string is : " + str (test_str))
delim = ":"
res = delim.join( sorted (test_str.split( ':' ), key = lambda x: x.lower()))
print ( "The sorted words : " + str (res))
|
Output
The original string is : gfg:is:best:for:geeks
The sorted words : best:for:geeks:gfg:is
Time complexity: O(n log n), where n is the number of words in the input string.
Auxiliary space: O(n), where n is the number of words in the input string.
Method #4: Using a list comprehension with sorted() and join()
This works by first splitting the input string using the specified delimiter. Then, the list comprehension iterates over each of the resulting words and puts them into a new list. This list is then sorted using the sorted() function. Finally, the sorted words are joined together with the delimiter using the join() function.
Python3
test_str = 'gfg:is:best:for:geeks'
delim = ':'
res = delim.join( sorted ([word for word in test_str.split(delim)]))
print ( "The sorted words : " + str (res))
|
Output
The sorted words : best:for:geeks:gfg:is
Time complexity: O(n log n), where n is the number of words in the input string.
Auxiliary space: O(n), where n is the number of words in the input string.
Method #5: Using the built-in function re.split() from the regular expression module
This method uses the re.split() function to split the string using the given delimiter, which returns a list of words. Then, the sort() function is used to sort the list of words in alphabetical order. Finally, the sorted words are joined using the delimiter and the result is printed.
Python3
import re
test_str = 'gfg:is:best:for:geeks'
delim = ":"
words = re.split(delim, test_str)
words.sort()
res = delim.join(words)
print ( "The original string is : " + test_str)
print ( "The sorted words : " + res)
|
Output
The original string is : gfg:is:best:for:geeks
The sorted words : best:for:geeks:gfg:is
Time complexity: O(n) where n is the length of the input string.
Auxiliary space: O(n) auxiliary space to store the list of words returned by re.split().
Method #6:Using itertools:
Algorithm :
- Initialize the input string test_str and delimiter delim.
- Split the input string into a list of words using the split() method and the delimiter delim. Store the result in a new list words.
- Sort the list of words using the sorted() method. The sorted() method creates a new sorted list of words and returns it.
- Create a new iterator using itertools.chain() and pass the sorted list of words as an argument. This creates a single iterator object that iterates over all the words in the sorted list.
- Join the words in the iterator using the join() method and the delimiter delim. This creates a new string that
- contains the sorted words separated by the delimiter.
- Print the sorted string.
Python3
import itertools
test_str = 'gfg:is:best:for:geeks'
print ( "The original string is : " + str (test_str))
delim = ":"
res = delim.join( sorted (itertools.chain(test_str.split(delim))))
print ( "The sorted words : " + str (res))
|
Output
The original string is : gfg:is:best:for:geeks
The sorted words : best:for:geeks:gfg:is
The time complexity : O(n log n), where n is the number of words in the input string.
The reason for this is that we first split the input string into a list of words using the split() method, which takes O(n) time, where n is the length of the input string. Then we sort this list of words using the sorted() method, which has a time complexity of O(n log n) in the worst case. Finally, we join the sorted list of words using the join() method, which takes O(n) time.
The auxiliary space :O(n), where n is the length of the input string. This is because we split the input string into a list of words, which requires O(n) space, and then we join the sorted list of words back into a string, which also requires O(n) space. The sorted() method creates a new list of words, so it also requires O(n) space. The itertools.chain() method does not create a new list, so it does not add any additional space complexity to the algorithm.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...