[英]Filtering out items from a list using nested list comprehensions in Python
I have two lists. 我有两个清单。 One contains sentences, the other contains words. 一个包含句子,另一个包含单词。
I want to have all the sentences, which do NOT contain any of the words from the list of words. 我想要所有的句子,不包含单词列表中的任何单词。
I'm trying to achieve this with list comprehensions. 我正试图用列表推导来实现这一点。 Example: 例:
cleared_sentences = [sentence for sentence in sentences if banned_word for word in words not in sentence]
However, it doesn't seem to be working as I get an error telling me that a variable is used before assignment. 但是,它似乎没有工作,因为我得到一个错误告诉我在赋值之前使用了一个变量。
I've tried looking for nested comprehensions and I am sure this must have been asked for but I can not find anything. 我一直试图寻找嵌套的理解,我确信这一定是被要求的,但我找不到任何东西。
How can I achieve this? 我怎样才能做到这一点?
You got the order mixed up: 你的订单混乱了:
[sentence for sentence in sentences for word in words if banned_word not in sentence]
Not that that'll work as that'll list the sentence
every time a banned word does show up in the sentence. 倒不是说会工作作为将列出这些sentence
的每一个禁忌词汇并在句子中出现的时间。 Take a look at the fully expanded nested loops version: 看看完全展开的嵌套循环版本:
for sentence in sentences:
for word in words:
if banned_word not in sentence:
result.append(sentence)
Use the any()
function to test for banned words instead: 使用any()
函数来测试禁止的单词:
[sentence for sentence in sentences if not any(banned_word in sentence for banned_word in words)]
any()
loops over the generator expression only until a True
value is found; any()
遍历生成器表达式,直到找到True
值; it'll stop doing work the moment a banned word is found in the sentence. 在句子中发现被禁词的那一刻,它就会停止工作。 This is more efficient at least. 这至少更有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.