如何从字符串列表中删除单词列表 [英] How to remove list of words from a list of strings
问题描述
很抱歉,这个问题有点令人困惑.这类似于此问题
Sorry if the question is bit confusing. This is similar to this question
我认为上述问题与我想要的问题很接近,但是在Clojure中.
I think this the above question is close to what I want, but in Clojure.
还有另一个问题
我需要类似这样的内容,但不是该问题中的"[br]",而是需要搜索和删除的字符串列表.
I need something like this but instead of '[br]' in that question, there is a list of strings that need to be searched and removed.
希望我能说清楚.
我认为这是由于python中的字符串是不可变的.
I think that this is due to the fact that strings in python are immutable.
我有一个需要从字符串列表中删除的干扰词列表.
I have a list of noise words that need to be removed from a list of strings.
如果我使用列表推导,最终将一次又一次地搜索相同的字符串.因此,仅删除"of"而不删除"the".所以我的修改后的列表看起来像这样
If I use the list comprehension, I end up searching the same string again and again. So, only "of" gets removed and not "the". So my modified list looks like this
places = ['New York', 'the New York City', 'at Moscow' and many more]
noise_words_list = ['of', 'the', 'in', 'for', 'at']
for place in places:
stuff = [place.replace(w, "").strip() for w in noise_words_list if place.startswith(w)]
我想知道我在做什么错误.
I would like to know as to what mistake I'm doing.
推荐答案
这是我的选择.这使用正则表达式.
Here is my stab at it. This uses regular expressions.
import re
pattern = re.compile("(of|the|in|for|at)\W", re.I)
phrases = ['of New York', 'of the New York']
map(lambda phrase: pattern.sub("", phrase), phrases) # ['New York', 'New York']
无人lambda
:
[pattern.sub("", phrase) for phrase in phrases]
更新
修复 gnibbler 指出的错误(谢谢!):
Fix for the bug pointed out by gnibbler (thanks!):
pattern = re.compile("\\b(of|the|in|for|at)\\W", re.I)
phrases = ['of New York', 'of the New York', 'Spain has rain']
[pattern.sub("", phrase) for phrase in phrases] # ['New York', 'New York', 'Spain has rain']
@prabhu:以上更改避免了从西班牙"中截取尾随的" in ".要验证是否对短语西班牙有雨"运行两个版本的正则表达式.
@prabhu: the above change avoids snipping off the trailing "in" from "Spain". To verify run both versions of the regular expressions against the phrase "Spain has rain".
这篇关于如何从字符串列表中删除单词列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!