I have two list containing full sentences and phrases:
my_sentence=['This is string', 'This is string too', 'That is not string', 'That are not sentence']
my_phrase=['This is', 'That is']
I try to find the string in my_sentence
that contains my_phrase
. For the sentence that is match, I split the sentence using my_phrase
and put the rest of sentences in another list, let say my_result
. However, I also want to save the sentence that is not match in my_result
.
The expected result in my_result
is:
my_result=['string','string too', 'not string', 'That are not sentence']
I have tried this code:
result=[]
for sentence in my_sentence:
for phrase in my_phrase:
if phrase in sentence:
res=sentence.split(phrase)
result.append(res)
else:
res=sentence
result.append(res)
print(result)
However, I got this result:
[['', ' string'], 'This is string', ['', ' string too'], 'This is string too', 'That is not string', ['', ' not string'], 'That are not sentence', 'That are not sentence']
Can somebody help me to fix my code?
Thank you in advance.
CodePudding user response:
You can try to use re
module:
import re
my_sentence = [
"This is string",
"This is string too",
"That is not string",
"That are not sentence",
]
my_phrase = ["This is", "That is"]
pat = re.compile("|".join(map(re.escape, my_phrase)))
out = [pat.sub("", s).strip() for s in my_sentence]
print(out)
Prints:
['string', 'string too', 'not string', 'That are not sentence']