开发者社区> 问答> 正文

如何有效地检查字符串是否包含两个列表中的至少一个元素

我有两个列表和一个句子列表如下。

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中这样做?

展开
收起
一码平川MACHEL 2019-01-22 17:30:27 2127 0
1 条回答
写回答
取消 提交回答
  • 您可以利用短路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']

    2019-07-17 23:26:22
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
用计算和数据去改变整个世界 立即下载
重新定义计算的边界 立即下载
低代码开发师(初级)实战教程 立即下载