使用 catch 将部分列表项匹配到另一个列表项
Perform partial one list items match into another with a catch
我有两个列表:
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]
您必须在条件表达式中使用单独的列表理解。在您的理解中,您为 j
中不存在的每个 i
添加了一个 j
for i in list_1
,这就是为什么您要重复的原因。
output = [j for j in list_2 if all([i not in j for i in list_1])]
您应该考虑尽可能为您的变量命名,这有助于您编写代码
你要的是
- 遍历句子
- 每次检查其中没有来自
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']
使用 set
路口怎么样?
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']
words = set(list_1)
output = [i for i in list_2 if not words & set(i.split())]
print(output)
输出:
['puppies are best']
您想检查短语中是否出现了任何单词,因此 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)
我有两个列表:
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]
您必须在条件表达式中使用单独的列表理解。在您的理解中,您为 j
中不存在的每个 i
添加了一个 j
for i in list_1
,这就是为什么您要重复的原因。
output = [j for j in list_2 if all([i not in j for i in list_1])]
您应该考虑尽可能为您的变量命名,这有助于您编写代码
你要的是
- 遍历句子
- 每次检查其中没有来自
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']
使用 set
路口怎么样?
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']
words = set(list_1)
output = [i for i in list_2 if not words & set(i.split())]
print(output)
输出:
['puppies are best']
您想检查短语中是否出现了任何单词,因此 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)