Because there is no way of assuming that
hello_2 corresponds to ., Test corresponds to Test, smth corresponds to test2, hello_1 corresponds to ., smth corresponds to test2 and hello_1 corresponds to ..
I will assume you would specify what are the matchs from each string or at least specify what words should replace the dots.
In the case you can specify every match, you could simply go over each of these matches and replace the first match, which value is ".". The code would look something like this:
def replace_words(string: str, matchs: list) -> str:
for key, value in matchs:
if value == ".":
string = string.replace(".", key, 1)
return string
base_str = "hello_2Testsmthhello_1smthhello_1"
change_str = ".Testtest2.test2."
matchs = [
("hello_2", "."),
("Test", "Test"),
("smth", "test2"),
("hello_1", "."),
("smth", "test2"),
("hello_1", ".")
]
print(replace_words(change_str, matchs))
Output:
hello_1Testtest2hello_2test2hello_1
In your example, though, you only specified some words. In that case you would need to first identify the order they appear in the string and their frequence and then replace them, as we did in the first example. The code would be something like this:
def replace_words2(string1: str, string2: str, words: list) -> str:
dots_num = string2.count(".")
replace_words = []
for word in words:
index = 0
while index != -1:
index = string1.find(word, index)
if index != -1:
replace_words.append((word, index))
string1 = string1.replace(word, "", 1)
replace_words.sort(key = lambda x : x[1])
replace_words = [i[0] for i in replace_words]
print(replace_words)
for word in replace_words:
string2 = string2.replace(".", word, 1)
return string2
base_str = "hello_2Testsmthhello_1smthhello_1"
change_str = ".Testtest2.test2."
words = ["hello_1", "hello_2"]
print(replace_words2(base_str, change_str, words))
If you would like the code to also figure out each match, the strings would need to have some kind of pattern. So, for instance, they could be "Hello_2TestSmthHello_1SmthHello_1" and ".TestTest2.Test2.". In that example we would be able to differentiate the words, as they are capitalized.