我有两个包含完整句子和短语的列表:
my_sentence=['This is string', 'This is string too', 'That is not string', 'That are not sentence']
my_phrase=['This is', 'That is']
我试着在my_sentence
中找到包含my_phrase
的字符串.对于匹配的句子,我用my_phrase
来拆分句子,并将其余的句子放在另一个列表中,假设是my_result
.但是,我也想保存my_result
中不匹配的句子.
my_result
年的预期结果是:
my_result=['string','string too', 'not string', 'That are not sentence']
我试过这个代码:
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)
然而,我得到了这样的结果:
[['', ' string'], 'This is string', ['', ' string too'], 'This is string too', 'That is not string', ['', ' not string'], 'That are not sentence', 'That are not sentence']
有人能帮我修正一下我的代码吗?
提前谢谢您.