Friday, December 27, 2024
Google search engine
HomeLanguagesPython – Replace K with Multiple values

Python – Replace K with Multiple values

Sometimes, while working with Python Strings, we can have a problem in which we need to perform replace of single character/work with particular list of values, based on occurrence. This kind of problem can have application in school and day-day programming. Let’s discuss certain ways in which this task can be performed.

Input : test_str = ‘* is * . It is recommended for *’, repl_char = ‘*’, repl_list = [‘Gfg’, ‘Best’, ‘CS’] 
Output : Gfg is Best . It is recommended for CS 

Input : test_str = ‘* is *’, repl_char = ‘*’, repl_list = [‘Gfg’, ‘Best’] 
Output : Gfg is Best

Method #1 : Using loop + replace() The combination of above functions can be used to solve this problem. In this, we perform the task of replacing using replace() and increase the index counter after each replacement. 

Python3




# Python3 code to demonstrate working of
# Replace K with Multiple values
# Using loop + replace()
 
# initializing strings
test_str = '_ is _ . It is recommended for _'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing repl_char
repl_char = '_'
 
# initializing repl_list
repl_list = ['Gfg', 'Best', 'CS']
 
# Replace K with Multiple values
# Using loop + replace()
for ele in repl_list:
    test_str = test_str.replace(repl_char, ele, 1)
 
# printing result
print("String after replacement : " + str(test_str))


Output : 

The original string is : _ is _ . It is recommended for _
String after replacement : Gfg is Best . It is recommended for CS

Time Complexity: O(n), where n is the length of the original string. 
Auxiliary Space: O(1), as the space used is constant and does not depend on the input size.

Method #2 : Using split() + join() + zip() The combination of above functions offer yet another way to solve this problem. In this, we first split the word using split(), zip both required lists using zip() and then rejoin with appropriate replacement. 

Python3




# Python3 code to demonstrate working of
# Replace K with Multiple values
# Using split() + join() + zip()
 
# initializing strings
test_str = '_ is _ . It is recommended for _'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing repl_char
repl_char = '_'
 
# initializing repl_list
repl_list = ['Gfg', 'Best', 'CS']
 
# Replace K with Multiple values
# Using split() + join() + zip()
test_list = test_str.split(repl_char)
temp = zip(test_list, repl_list)
res = ''.join([ele for sub in temp for ele in sub])
 
# printing result
print("String after replacement : " + str(res))


Output : 

The original string is : _ is _ . It is recommended for _
String after replacement : Gfg is Best . It is recommended for CS

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

Method #3: Using regular expressions and a lambda function

Steps:

  1. Import the re module to use regular expressions.
  2. Define a lambda function that takes a match object as input and returns the corresponding replacement value from repl_list.
  3. Use the re.sub() function to replace all occurrences of repl_char in test_str with the values from repl_list using the lambda function.
  4. Store the result in res.

Python3




import re
 
# initializing strings
test_str = '_ is _ . It is recommended for _'
 
# printing original string
print("The original string is : " + str(test_str))
 
# initializing repl_char
repl_char = '_'
 
# initializing repl_list
repl_list = ['Gfg', 'Best', 'CS']
 
# Replace K with Multiple values
# Using regular expressions and a lambda function
res = re.sub(repl_char, lambda x: repl_list.pop(0), test_str)
 
# printing result
print("String after replacement : " + str(res))


Output

The original string is : _ is _ . It is recommended for _
String after replacement : Gfg is Best . It is recommended for CS

Time complexity: O(n)
Auxiliary space: O(n)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments