问题描述
我有两个列表:
list_1 = ['world','abc','bcd','ghy','car','hell','rock']
list_2 = ['the world is big','i want a car','puppies are best','you rock the world']
我想检查 list_1 的单词是否以任何形状或形式存在于 list_2 中,然后简单地从 list_2 中删除整个句子,最后打印 list_2
例如:
the word 'world' from list_1 should take out the sentence 'the world is big' from list_2
the word 'car' from list_2 should take out the sentence 'i want a car'
我尝试过像这样使用列表理解,但遗憾的是它重复了
output = [j for i in list_1 for j in list_2 if i not in j]
解决方法
如果可以,您应该考虑为变量赋予有意义的名称,这有助于您编写代码
你想要的是
- 遍历句子
- 每次检查没有来自
list_1
的单词
output = [sentence for sentence in list_2
if all(word not in sentence for word in list_1)]
print(output) # ['puppies are best']
,
您必须在条件表达式中使用单独的列表推导式。在您的理解中,您为每个不在 j
中的 for i in list_1
添加了一个 i
j
,这就是您得到重复的原因。
output = [j for j in list_2 if all([i not in j for i in list_1])]
,
您想检查短语中是否出现任何单词,因此 any
是要走的路。在我看来,这比使用 all
和否定检查更具可读性。
words = ['world','abc','bcd','ghy','car','hell','rock']
phrases = ['the world is big','i want a car','puppies are best','you rock the world']
result = [phrase for phrase in phrases if not any(word in phrase for word in words)]
print(result)
你得到 ['puppies are best']
。
解决方案大致相当于:
result = []
for phrase in phrases:
contains_any_word = False
for word in words:
if word in phrase:
contains_any_word = True
break
if not contains_any_word:
result.append(phrase)
,
如何使用 set
交叉口?
list_1 = ['world','rock']
list_2 = ['the world is big','you rock the world']
words = set(list_1)
output = [i for i in list_2 if not words & set(i.split())]
print(output)
输出:
['puppies are best']