代码之家  ›  专栏  ›  技术社区  ›  Carrol

将拆分字符串与列表中的部分单词相交(可能与正则表达式相交)

  •  1
  • Carrol  · 技术社区  · 4 年前

    我必须列出:

    keywords = ['critic', 'argu', 'dog', 'cat']
    splitSentences = ['Add', 'critical', 'argument', 'birds']
    

    我要查一查这里面有多少字 splitSentence 从…开始 keywords .在我的例子中,那就是 2 (用于 批评的 匹配“评论家”和 论点 匹配“argu”)。

    问题在于 set(keywords).intersection(splitSentences) 返回 0 .我试着在每个单词前面加前缀 关键词 具有 ^ ,但它仍然返回0。

    抱歉,这是Python的新功能。我在写一本Jupyter笔记本。

    2 回复  |  直到 4 年前
        1
  •  1
  •   BiRD    4 年前

    使用正则表达式:

    import re
    
    for i in keywords:
        count = 0
        pref = '^'+ i
        for word in splitSentences:
            if re.match(pref, word):
                count += 1
        print(count)
    

    半单轮:

    for i in keywords:
        print(sum([1 for word in splitSentences if word.startswith(i)]))
    

    一行:

    print({el:sum([1 for word in splitSentences if word.startswith(el)]) for el in keywords})
    
        2
  •  0
  •   Harshal Parekh Cath    4 年前
    keywords = ['critic', 'argu', 'dog', 'cat']
    splitSentences = ['Add', 'critical', 'argument', 'birds']
    
    for s in splitSentences:
      for k in keywords:
        if s.startswith(k):
          print(s)
    

    几乎不言自明。重复 splitSentences 每一个单词 分句 重复 keywords 并检查它是否以关键字开头。

    一行:

    [s for k in keywords for s in splitSentences if s.startswith(k)]
    

    时间复杂性: O(sk) . Trie 数据结构将更加高效: O(s + k)

    推荐文章