如何过滤python中的单词?

如何过滤python中的单词?,python,string,sorting,Python,String,Sorting,例如: item =['the dog is gone', 'the dog and cat is gone'] words= ['dog','cat'] 我希望能够过滤掉狗和猫,这样它就会显示: item=['the is gone', 'the and is gone'] 我得到以下信息: ['the is gone', 'the cat and is gone', 'the and dog is gone'] 您正在循环每个单词的所有行,并附加替换项。您应该切换这些

例如:

item =['the dog is gone', 'the dog and cat is gone']
words= ['dog','cat'] 
我希望能够过滤掉
,这样它就会显示:

item=['the  is gone', 'the   and  is gone']

我得到以下信息:

['the  is gone', 'the cat and  is gone', 'the  and dog is gone']

您正在循环每个单词的所有行,并附加替换项。您应该切换这些循环:

item1 = [] 
for line in item:
    for w in words:
        line = line.replace(w, '')
    item1.append(line)
注意:我修改了一些代码

  • gg
    更改为
    line
  • it
    更改为
    item
  • 删除了检查
    行是否包含
    w
    ,因为这是由
    replace
replace
不知道单词边界。如果您只想删除整个单词,您应该尝试另一种方法。使用


您正在循环每个单词的所有行,并附加替换项。您应该切换这些循环:

item1 = [] 
for line in item:
    for w in words:
        line = line.replace(w, '')
    item1.append(line)
注意:我修改了一些代码

  • gg
    更改为
    line
  • it
    更改为
    item
  • 删除了检查
    行是否包含
    w
    ,因为这是由
    replace
replace
不知道单词边界。如果您只想删除整个单词,您应该尝试另一种方法。使用


您可以改用这种方法:

item =['the dog is gone', 'the dog and cat is gone']
words= ['dog','cat'] 

item2 = [" ".join([w for w in t.split() if not w in words]) for t in item]

print item2

>>> ['the is gone', 'the and is gone']

您可以改用这种方法:

item =['the dog is gone', 'the dog and cat is gone']
words= ['dog','cat'] 

item2 = [" ".join([w for w in t.split() if not w in words]) for t in item]

print item2

>>> ['the is gone', 'the and is gone']

当这样做其他单词时,它会分解单词ie.good->od但go==good进行错误的比较。@用户1753878仅用替换完整单词更新答案当这样做其他单词时,它会分解单词ie.good->od但go==good进行错误的比较。@user1753878仅用替换完整单词更新答案这是明显更快!对于我的用例来说,微秒比毫秒要快得多!我的用例是微秒还是毫秒