Thursday, July 11, 2024
HomeLanguagesPythonPython – Replace words from Dictionary

Python – Replace words from Dictionary

Given String, replace it’s words from lookup dictionary.

Input : test_str = ‘geekforLazyroar best for Lazyroar’, repl_dict = {“Lazyroar” : “all CS aspirants”} 
Output : geekforLazyroar best for all CS aspirants 
Explanation : “Lazyroar” word is replaced by lookup value. 

Input : test_str = ‘geekforLazyroar best for Lazyroar’, repl_dict = {“good” : “all CS aspirants”} 
Output : geekforLazyroar best for Lazyroar 
Explanation : No lookup value, unchanged result.

Method #1 : Using split() + get() + join()

In this, we initially split the list using split(), then look for lookups using get(), and if found, replaced and joined back to string using join().

Python3




# Python3 code to demonstrate working of
# Replace words from Dictionary
# Using split() + join() + get()
 
# initializing string
test_str = 'geekforLazyroar best for Lazyroar'
 
# printing original string
print("The original string is : " + str(test_str))
 
# lookup Dictionary
lookp_dict = {"best" : "good and better", "Lazyroar" : "all CS aspirants"}
 
# performing split()
temp = test_str.split()
res = []
for wrd in temp:
     
    # searching from lookp_dict
    res.append(lookp_dict.get(wrd, wrd))
     
res = ' '.join(res)
 
# printing result
print("Replaced Strings : " + str(res))


Output

The original string is : geekforLazyroar best for Lazyroar
Replaced Strings : geekforLazyroar good and better for all CS aspirants

Time Complexity: O(N), where N is the length of the input string.

Auxiliary Space: O(N)

Method #2 : Using list comprehension + join()

Similar to above method, difference just being 1 liner rather than 3-4 steps in separate lines.

Python3




# Python3 code to demonstrate working of
# Replace words from Dictionary
# Using list comprehension + join()
 
# initializing string
test_str = 'geekforLazyroar best for Lazyroar'
 
# printing original string
print("The original string is : " + str(test_str))
 
# lookup Dictionary
lookp_dict = {"best" : "good and better", "Lazyroar" : "all CS aspirants"}
 
# one-liner to solve problem
res = " ".join(lookp_dict.get(ele, ele) for ele in test_str.split())
 
# printing result
print("Replaced Strings : " + str(res))


Output

The original string is : geekforLazyroar best for Lazyroar
Replaced Strings : geekforLazyroar good and better for all CS aspirants

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

Method #3: Using a for loop and a temporary list

  • Initialize the lookup dictionary variable lookp_dict with the key-value pairs “best” : “good and better” and “Lazyroar” : “all CS aspirants”.
  • Create an empty list called temp that will hold the replaced strings.
  • Split the input string into a list of words using the split() method and iterate over each word using a for loop.
  • For each word, check if it exists in the lookup dictionary using the get() method. If it does, append the corresponding value to the temp list, otherwise append the original word.
  • Join the temp list using the join() method and separate the words with a space character. Assign this string to the variable res.
  • Print the final result using print(“Replaced Strings : ” + str(res)).

Python3




# initializing string
test_str = 'geekforLazyroar best for Lazyroar'
 
# printing original string
print("The original string is : " + str(test_str))
 
# lookup Dictionary
lookp_dict = {"best" : "good and better", "Lazyroar" : "all CS aspirants"}
 
# create a temporary list to hold the replaced strings
temp = []
for word in test_str.split():
    temp.append(lookp_dict.get(word, word))
 
# join the temporary list to create the final output string
res = " ".join(temp)
 
# printing result
print("Replaced Strings : " + str(res))


Output

The original string is : geekforLazyroar best for Lazyroar
Replaced Strings : geekforLazyroar good and better for all CS aspirants

Time complexity: O(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 (due to the creation of the temporary list).

Dominic Rubhabha Wardslaus
Dominic Rubhabha Wardslaushttps://neveropen.dev
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments