What is an efficient way to erase substrings?

Posted by Legend on Stack Overflow See other posts from Stack Overflow or by Legend
Published on 2011-11-15T07:37:47Z Indexed on 2011/11/15 17:50 UTC
Read the original article Hit count: 229

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about python

Related posts about string