Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace a word in a string if it is in a list of words

Tags:

python

string

I have the following string:

s = 'in may 1999, nothing really happened, same as in june 1999'

month_set = {'may', 'june', 'july'}

I want to replace all words that match words in the month_set list with the word month. The output should look like

'in month 1999, nothing really happened, same as in month 1999'

The

for month in month_set:
    sentence = s.replace(month, 'date')
    print(sentence)

but that returned the following:

in may 1999, nothing really happened, same as in june 1999
in date 1999, nothing really happened, same as in june 1999
in may 1999, nothing really happened, same as in date 1999

Beside, if the above works, I need to apply it to a big list of strings which will make it slow, I think.

like image 637
mallet Avatar asked Jan 31 '26 05:01

mallet


2 Answers

You can try this:

s = 'in may 1999, nothing really happened, same as in june 1999'
month_set = {'may', 'june', 'july'}
final_string = ' '.join("month" if i in month_set else i for i in s.split())

Output:

'in month 1999, nothing really happened, same as in month 1999'

A pure regex solution:

import re
s = 'in may 1999, nothing really happened, same as in june 1999'
month_set = {'may', 'june', 'july'}
final_string = re.sub('|'.join("(?<=\s){}(?=\s)".format(i) for i in month_set), 'month', s)
print(final_string)
s1 = 'may june mayor'
final_string1 = re.sub('|'.join("((?<=\s)|(?<=^)){}((?=\s)|(?=$))".format(i) for i in month_set), 'month', s1)
print(final_string1)

Output:

'in month 1999, nothing really happened, same as in month 1999'
'month month mayor'
like image 188
Ajax1234 Avatar answered Feb 02 '26 18:02

Ajax1234


Use regex:

import re
months = ['May', 'June']
myRegex = re.compile("|".join(months))
myRegex.sub("Month", "Welcome to May, next is June")

This version is case-sensitive but if you want case-insensitive version just use:

myRegex = re.compile("|".join(months), re.IGNORECASE)
like image 29
fievel Avatar answered Feb 02 '26 17:02

fievel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!