How to compare two strings and replace characters at a specific index?

Viewed 375

Say I have a string:

test_str = 'hello_2Testsmthhello_1smthhello_1'

And another string:

w = '.Testtest2.test2.'

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 ..

Now I have a list of indices where I have replaced the hello_1 and hello_1 in test_str with ..

I have done the above as follows:

list_of_indices = [i for i, x in enumerate(list(test_str)) if x == "."]
# print (list_of_indices) --> [0, 9, 14]

How do I compare the two strings as mentioned above and replace the . in the second string with the word(s) in the first string at that specific index?

Expected output:

hello_2Testtest2hello_1test2hello_1

Here's what I've tried so far:

replace_from_list = ['hello_1', 'hello_2']
for n in remove_from_list:
    for x in list_of_indices:
        ???
print (w)
1 Answers

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.

Related