[英]Filter list of strings to not contain any of the string from another list as a substring
[英]Two Lists of strings: remove strings from list A that contain any string from list B?
我有两个字符串列表。
filters = ['foo', 'bar']
wordlist = ['hey', 'badge', 'foot', 'bar', 'cone']
我想删除单词列表中包含过滤器的每个单词。
def filter_wordlist(filters, wordlist):
for word in wordlist:
if word contains any string from filters, remove it from the wordlist
return wordlist
因此,此过滤器函数将返回['hey', 'badge', 'cone']
。 它删除了bar
因为bar
在filters
。 它删除了foot
因为其中包含字符串foo
。
我尝试了这个:
for word in wordlist:
for f in filters:
if f in word:
wordlist.remove(word)
但是它始终返回ValueError: list.remove(x): x not in list
。 因此,我尝试将其包装在一系列越来越令人沮丧的try / except块中,但是在地鼠洞中没有任何作用。 我在remove命令下添加了break
语句,但这很...参差不齐。 似乎wordlist
末尾的项目未正确过滤。
所以我改变了策略:
for f in filters:
for word in wordlist:
if f in word:
wordlist.remove(word)
就像以前一样,这参差不齐。
所以我尝试了这个:
for word in wordlist:
if any(f in word for f in filters):
wordlist.remove(word)
现在,这肯定让我很恼火。 参差不齐。 到现在为止,我已经意识到发生了什么-在我遍历列表时使用remove()
更改了列表,这搞砸了迭代。
这似乎应该很简单。 我有两个字符串列表。 取出列表A中的所有项目。如果这些项目中的任何一个包含列表B中的任何项目,请从列表A中删除该项目。
这是我终于得到的有效解决方案:
keepitup = True
while keepitup:
start_length = len(wordlist)
for word in wordlist:
if any(f in word for f in filters):
wordlist.remove(link)
end_length = len(wordlist)
if start_length != end_length:
keepitup = True
else:
keepitup = False
这似乎很荒谬。 当然有更好的方法吗?
您可以使用列表理解:
wordlist = [word for word in wordlist if all(f not in word for f in filters)]
或过滤功能:
filter(lambda word: all(f not in word for f in filters), wordlist)
或者您可以遍历单词表的副本:
for word in wordlist[:]:
if any(f in word for f in filters):
wordlist.remove(word)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.