Open In App

Python – Replace multiple words with K

Last Updated : 12 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Replace multiple words with K
# Using join() + split() + list comprehension
 
# initializing string
test_str = 'Geeksforgeeks is best for geeks and CS'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing word list
word_list = ["best", 'CS', 'for']
 
# initializing replace word
repl_wrd = 'gfg'
 
# Replace multiple words with K
# Using join() + split() + list comprehension
res = ' '.join([repl_wrd if idx in word_list else idx for idx in test_str.split()])
 
# printing result
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




# Python3 code to demonstrate working of
# Replace multiple words with K
# Using regex + join()
import re
 
# initializing string
test_str = 'Geeksforgeeks is best for geeks and CS'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing word list
word_list = ["best", 'CS', 'for']
 
# initializing replace word
repl_wrd = 'gfg'
 
# Replace multiple words with K
# Using regex + join()
res = re.sub("|".join(sorted(word_list, key = len, reverse = True)), repl_wrd, test_str)
 
# printing result
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




# Python3 code to demonstrate working of
# Replace multiple words with K
 
 
# initializing string
test_str = 'Geeksforgeeks is best for geeks and CS'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing word list
word_list = ["best", 'CS', 'for']
 
# initializing replace word
repl_wrd = 'gfg'
 
# Replace multiple words with K
for i in word_list:
    test_str=test_str.replace(i,repl_wrd)
# printing result
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):
# create a regular expression pattern from the dictionary keys
  pattern = re.compile("|".join(map(re.escape, word_dict.keys())))
  # use re.sub to replace the keys with values in the text
  return pattern.sub(lambda x: word_dict[x.group(0)], text)
 
#initialize the input string and dictionary of words to replace
text = 'Geeksforgeeks is best for geeks and CS'
word_dict = {"best": "gfg", "CS": "gfg", "for": "gfg"}
 
#replace the words in the text using the dictionary
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:

  1. Import the reduce() method from functools module.
  2. 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.
  3. 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.
  4. The final value of the string after multiple replacements is returned as output.

Python3




from functools import reduce
 
# initializing string
test_str = 'Geeksforgeeks is best for geeks and CS'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing word list
word_list = ["best", 'CS', 'for']
 
# initializing replace word
repl_wrd = 'gfg'
 
# Replace multiple words with K
replace_func = lambda s, w: s.replace(w, repl_wrd)
test_str = reduce(replace_func, word_list, test_str)
 
# printing result
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:

  1. Initialize the input string and list of words to replace.
  2. 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.
  3. Heapify the heap using the heapq.heapify() method.
  4. While the heap is not empty, pop the word with the largest length from the heap using heapq.heappop().
  5. Replace all occurrences of the popped word in the input string with the replacement word.
  6. Repeat steps 4 and 5 until the heap is empty.
  7. Return the modified input string.

Python3




import heapq
 
# initialize the input string and list of words to replace
text = 'Geeksforgeeks is best for geeks and CS'
word_list = ["best", 'CS', 'for']
repl_wrd = 'gfg'
# printing original string
print("The original string is : " + str(text))
# create a heap from the word_list
heap = [(-len(word), word, repl_wrd) for word in word_list]
heapq.heapify(heap)
 
# replace the words in the text using heapq
while heap:
    _, word, repl = heapq.heappop(heap)
    text = text.replace(word, repl)
 
print("String after multiple replace : " + str(text))
#This code is contributed by Pushpa.
 
#This code is contributed by Pushpa.


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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads