use*_*878 1 python sorting string
例如:
item =['the dog is gone', 'the dog and cat is gone']
words= ['dog','cat']
Run Code Online (Sandbox Code Playgroud)
我希望能够过滤掉它dog,cat所以它会读取:
item=['the is gone', 'the and is gone']
Run Code Online (Sandbox Code Playgroud)
item1=[]
for w in words:
for line in item:
if w in line:
j=gg.replace(it,'')
item1.append(j)
Run Code Online (Sandbox Code Playgroud)
我得到以下内容:
['the is gone', 'the cat and is gone', 'the and dog is gone']
Run Code Online (Sandbox Code Playgroud)
你循环遍历每个单词的所有行并附加替换.你应该切换这些循环:
item1 = []
for line in item:
for w in words:
line = line.replace(w, '')
item1.append(line)
Run Code Online (Sandbox Code Playgroud)
注意:我改变了一些代码
gg了lineit了itemline包含w由处理的replacereplace不了解单词边界.如果您只想删除整个单词,您应该尝试不同的方法.运用re.sub
import re
item1 = []
for line in item:
for w in words:
line = re.sub(r'\b%s\b' % w, '', line) # '\b' is a word boundry
item1.append(line)
Run Code Online (Sandbox Code Playgroud)