我有两个列表和一个句子列表如下。
list1 = ['data mining', 'data sources', 'data']
list2 = ['neural networks', 'deep learning', 'machine learning']
sentences = ["mining data using neural networks has become a trend", "data mining is easy with python", "machine learning is my favorite", "data mining and machine learning are awesome", "data sources and data can been used for deep learning purposes", "data, deep learning and neural networks"]
我想挑选具有两个元素的句子list1和list2。即输出应该是;
["mining data using neural networks has become a trend", "data mining and machine learning are awesome", "data sources and data can been used for deep learning purposes", "data, deep learning and neural networks"]
我目前的代码如下。
for sentence in sentences:
for terms in list1:
for words in list2:
if terms in sentence:
if words in sentence:
print(sentence)
但是,代码是O(n ^ 3)并且效率不高。有没有有效的方法在python中这样做?
您可以利用短路all并any改善性能:
list1 = ['data mining', 'data sources', 'data']
list2 = ['neural networks', 'deep learning', 'machine learning']
sentences = ["mining data using neural networks has become a trend", "data mining is easy with python", "machine learning is my favorite", "data mining and machine learning are awesome", "data sources and data can been used for deep learning purposes", "data, deep learning and neural networks"]
for sentence in sentences:
if all(any(term in sentence for term in lst) for lst in (list1, list2)):
print(sentence)
集合比列表更有效。如果要查找包含两个列表中单个单词的句子,您可以&使用两个“列表” 检查每个句子的intersection(),而不是使用嵌套循环:
list1 = set(list1)
list2 = set(list2)
[sentence for sentence in set(sentences.split()) if sentence & list1 & list2]
但是,由于您的列表似乎包含短语(或单词序列),因此可能很难避免使用多个循环。如果找到或找不到匹配项,您至少可以中断或继续循环。也没有必要在彼此之间嵌套您要匹配的两个列表的循环。
result = []
for sentence in sentences:
for word in list1:
if word in sentence:
break
else:
continue
for word in list2:
if word in sentence:
break
else:
continue
result.append(sentence)
结果:
['mining data using neural networks has become a trend',
'data mining and machine learning are awesome',
'data sources and data can been used for deep learning purposes',
'data, deep learning and neural networks']
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。