Python – Replace multiple words with K
Last Updated :
12 May, 2023
Sometimes, while working with Python strings, we can have a problem in which we need to perform a replace of multiple words with a single word. This can have application in many domains including day-day programming and school programming. Lets discuss certain ways in which this task can be performed.
Method #1 : Using join() + split() + list comprehension The combination of above functions can be used to perform this task. In this, we split the string into words, check and replace the list words using join and list comprehension.
Python3
test_str = 'Geeksforgeeks is best for geeks and CS'
print ( "The original string is : " + str (test_str))
word_list = [ "best" , 'CS' , 'for' ]
repl_wrd = 'gfg'
res = ' ' .join([repl_wrd if idx in word_list else idx for idx in test_str.split()])
print ( "String after multiple replace : " + str (res))
|
Output :
The original string is : Geeksforgeeks is best for geeks and CS
String after multiple replace : Geeksforgeeks is gfg gfg geeks and gfg
Time complexity: O(n), where n is the number of words in the string “test_str”.
Auxiliary space: O(m), where m is the number of characters in the output string “res”. This is because the output string requires memory proportional to its length to store its characters.
Method #2: Using regex + join() The combination of above functions can be used to perform this task. In this, we find the words using regex and perform the replace using join() and list comprehension.
Python3
import re
test_str = 'Geeksforgeeks is best for geeks and CS'
print ( "The original string is : " + str (test_str))
word_list = [ "best" , 'CS' , 'for' ]
repl_wrd = 'gfg'
res = re.sub( "|" .join( sorted (word_list, key = len , reverse = True )), repl_wrd, test_str)
print ( "String after multiple replace : " + str (res))
|
Output :
The original string is : Geeksforgeeks is best for geeks and CS
String after multiple replace : Geeksforgeeks is gfg gfg geeks and gfg
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using for loop+replace() methods
Python3
test_str = 'Geeksforgeeks is best for geeks and CS'
print ( "The original string is : " + str (test_str))
word_list = [ "best" , 'CS' , 'for' ]
repl_wrd = 'gfg'
for i in word_list:
test_str = test_str.replace(i,repl_wrd)
print ( "String after multiple replace : " + str (test_str))
|
Output
The original string is : Geeksforgeeks is best for geeks and CS
String after multiple replace : Geeksgfggeeks is gfg gfg geeks and gfg
Time Complexity: O(n*m) where n is the length of the input string and m is the number of words in the word_list.
Auxiliary Space: O(n) as it only uses memory for storing the input string and the list of words.
Approach 4: Using dictionary and re.sub()
Python3
import re
def multiple_replace(text, word_dict):
pattern = re. compile ( "|" .join( map (re.escape, word_dict.keys())))
return pattern.sub( lambda x: word_dict[x.group( 0 )], text)
text = 'Geeksforgeeks is best for geeks and CS'
word_dict = { "best" : "gfg" , "CS" : "gfg" , "for" : "gfg" }
result = multiple_replace(text, word_dict)
print ( "String after multiple replace : " + str (result))
|
Output
String after multiple replace : Geeksgfggeeks is gfg gfg geeks and gfg
Time Complexity: O(n), where n is the number of characters in the input string.
Auxiliary Space: O(n), as the pattern and result strings will require memory proportional to their length.
Approach #5: Using a lambda function with reduce() method.
We can use a lambda function with the reduce() method to replace multiple words in the given string with the specified word. The reduce() method takes a function and a sequence as input and applies the function cumulatively to the sequence elements from left to right, so that it reduces the sequence to a single value.
Step-by-step approach:
- Import the reduce() method from functools module.
- Define a lambda function that takes two parameters: a string and a word from the word_list, and replaces the word with the specified replacement word.
- Pass this lambda function and the word_list as input to the reduce() method, and apply it cumulatively to the given string from left to right.
- The final value of the string after multiple replacements is returned as output.
Python3
from functools import reduce
test_str = 'Geeksforgeeks is best for geeks and CS'
print ( "The original string is : " + str (test_str))
word_list = [ "best" , 'CS' , 'for' ]
repl_wrd = 'gfg'
replace_func = lambda s, w: s.replace(w, repl_wrd)
test_str = reduce (replace_func, word_list, test_str)
print ( "String after multiple replace : " + str (test_str))
|
Output
The original string is : Geeksforgeeks is best for geeks and CS
String after multiple replace : Geeksgfggeeks is gfg gfg geeks and gfg
Time complexity: O(nk), where n is the length of the given string and k is the length of the word list.
Auxiliary space: O(n), where n is the length of the given string.
Approach 6: Using heapq:
Algorithm:
- Initialize the input string and list of words to replace.
- Create a heap from the word_list by converting each word into a tuple of the form (-len(word), word, repl_wrd), where -len(word) is the length of the word multiplied by -1 to make the heap behave like a max heap.
- Heapify the heap using the heapq.heapify() method.
- While the heap is not empty, pop the word with the largest length from the heap using heapq.heappop().
- Replace all occurrences of the popped word in the input string with the replacement word.
- Repeat steps 4 and 5 until the heap is empty.
- Return the modified input string.
Python3
import heapq
text = 'Geeksforgeeks is best for geeks and CS'
word_list = [ "best" , 'CS' , 'for' ]
repl_wrd = 'gfg'
print ( "The original string is : " + str (text))
heap = [( - len (word), word, repl_wrd) for word in word_list]
heapq.heapify(heap)
while heap:
_, word, repl = heapq.heappop(heap)
text = text.replace(word, repl)
print ( "String after multiple replace : " + str (text))
|
Output
The original string is : Geeksforgeeks is best for geeks and CS
String after multiple replace : Geeksgfggeeks is gfg gfg geeks and gfg
Time complexity: O(n log n) because it uses the heapq.heapify() method to create a heap with n elements, and then it pops elements from the heap n times, each time taking O(log n) time.
Auxiliary Space: O(n) because it creates a heap with n elements.
Please Login to comment...