What is an efficient way to erase substrings?
- by Legend
I have a long string and a set of <end-index, string> list like the following:
long_sentence = "This is a long long long long sentence"
indices = [[6, "is"], [8, "is a"], [18, "long"], [23, "long"]]
An element 6, "is" indicates that 6 is the end index of the word "is" in the string. I want to get the following string in the end:
>> print long_sentence
This .... long ......... long sentence"
I tried an approach like this:
temp = long_sentence
for i in indices:
temp = temp[:int(i[0]) - len(i[1])] + '.'*(len(i[1])+1) + temp[i[0]+1:]
While this seems to be working, it is taking exceptionally long time (more than 6 hours on 5000 strings inside a 300 MB file). Is there a way to speed this up?