2
\$\begingroup\$

I'm working with a dictionary with 11830 keys/values, that have to loop over 13905 strings, that can have up to 50 words each. My series looks like this:

  strings
"hello foo helloo"
"bye bar byer"

And my dictionary like this:

dic={'hello':'hi','bye':'byebye'}

So I want my final Series to look like this:

  strings
"hi foo helloo"
"byebye bar byer"

I achieved that like this:

for k,v in dicc.items():
    b=b.apply(lambda x: re.sub(r'\b'+k+r'\b',r'\b'+v+r'\b',x))

But it's quite slow. Is there a faster way to do it?

Edit: noticed splitting the string didn't solve the issue of replacing substrings that matched the dictionary.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.