Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Sort words separated by Delimiter

Python – Sort words separated by Delimiter

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:Lazyroar', delim = "*" 
Output : best*for*Lazyroar*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




# Python3 code to demonstrate working of
# Sort words separated by Delimiter
# Using split() + join() + sorted()
 
# initializing string
test_str = 'gfg:is:best:for:Lazyroar'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Delimiter
delim = ":"
 
# joining the sorted string after split
res = delim.join(sorted(test_str.split(':')))
 
# printing result
print("The sorted words : " + str(res))


Output

The original string is : gfg:is:best:for:Lazyroar
The sorted words : best:for:Lazyroar:gfg:is

Time Complexity: O(n) 
Space Complexity: O(n)

Method #2: Using sort() function

Python3




# Python3 code to demonstrate working of
# Sort words separated by Delimiter
 
# initializing string
test_str = 'gfg:is:best:for:Lazyroar'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Delimiter
delim = ":"
test_list = test_str.split(delim)
test_list.sort()
# joining the sorted string after split
res = delim.join(test_list)
 
# printing result
print("The sorted words : " + str(res))


Output

The original string is : gfg:is:best:for:Lazyroar
The sorted words : best:for:Lazyroar: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




# initializing string
test_str = 'gfg:is:best:for:Lazyroar'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing Delimiter
delim = ":"
 
# joining the sorted string after split using lambda function
res = delim.join(sorted(test_str.split(':'), key=lambda x: x.lower()))
 
# printing result
print("The sorted words : " + str(res))


Output

The original string is : gfg:is:best:for:Lazyroar
The sorted words : best:for:Lazyroar: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:Lazyroar'
delim = ':'
 
# Using a list comprehension with sorted() and join()
res = delim.join(sorted([word for word in test_str.split(delim)]))
 
print("The sorted words : " + str(res))


Output

The sorted words : best:for:Lazyroar: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
 
# initializing string
test_str = 'gfg:is:best:for:Lazyroar'
 
# initializing Delimiter
delim = ":"
 
# split the string using delimiter
words = re.split(delim, test_str)
 
# sort the list of words
words.sort()
 
# join the sorted words with delimiter
res = delim.join(words)
 
# printing original string and sorted words
print("The original string is : " + test_str)
print("The sorted words : " + res)


Output

The original string is : gfg:is:best:for:Lazyroar
The sorted words : best:for:Lazyroar: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 :

  1. Initialize the input string test_str and delimiter delim.
  2. 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.
  3. Sort the list of words using the sorted() method. The sorted() method creates a new sorted list of words and returns it.
  4. 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.
  5. Join the words in the iterator using the join() method and the delimiter delim. This creates a new string that
  6. contains the sorted words separated by the delimiter.
  7. Print the sorted string.

Python3




import itertools
# initializing string
test_str = 'gfg:is:best:for:Lazyroar'
# printing original string
print("The original string is : " + str(test_str))
  
delim = ":"
res = delim.join(sorted(itertools.chain(test_str.split(delim))))
# printing result
print("The sorted words : " + str(res))
#This code is contributed by Jyothi Pinjala.


Output

The original string is : gfg:is:best:for:Lazyroar
The sorted words : best:for:Lazyroar: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.

RELATED ARTICLES

Most Popular

Recent Comments