Python – Replace words from Dictionary
Last Updated :
03 May, 2023
Given String, replace it’s words from lookup dictionary.
Input : test_str = ‘geekforgeeks best for geeks’, repl_dict = {“geeks” : “all CS aspirants”}
Output : geekforgeeks best for all CS aspirants
Explanation : “geeks” word is replaced by lookup value.
Input : test_str = ‘geekforgeeks best for geeks’, repl_dict = {“good” : “all CS aspirants”}
Output : geekforgeeks best for geeks
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
test_str = 'geekforgeeks best for geeks'
print ( "The original string is : " + str (test_str))
lookp_dict = { "best" : "good and better" , "geeks" : "all CS aspirants" }
temp = test_str.split()
res = []
for wrd in temp:
res.append(lookp_dict.get(wrd, wrd))
res = ' ' .join(res)
print ( "Replaced Strings : " + str (res))
|
Output
The original string is : geekforgeeks best for geeks
Replaced Strings : geekforgeeks 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
test_str = 'geekforgeeks best for geeks'
print ( "The original string is : " + str (test_str))
lookp_dict = { "best" : "good and better" , "geeks" : "all CS aspirants" }
res = " " .join(lookp_dict.get(ele, ele) for ele in test_str.split())
print ( "Replaced Strings : " + str (res))
|
Output
The original string is : geekforgeeks best for geeks
Replaced Strings : geekforgeeks 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 “geeks” : “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
test_str = 'geekforgeeks best for geeks'
print ( "The original string is : " + str (test_str))
lookp_dict = { "best" : "good and better" , "geeks" : "all CS aspirants" }
temp = []
for word in test_str.split():
temp.append(lookp_dict.get(word, word))
res = " " .join(temp)
print ( "Replaced Strings : " + str (res))
|
Output
The original string is : geekforgeeks best for geeks
Replaced Strings : geekforgeeks 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).
Please Login to comment...