【问题标题】:How do I check if a list of strings is in another list of strings?如何检查字符串列表是否在另一个字符串列表中?
【发布时间】:2018-06-06 22:58:42
【问题描述】:

例子

description = ['This is a random sentence. I like to travel and stuff','Hello world', 'Things on my bucket list, travel']

my_list = ['travel','fire']

我想检查 my_list 上的任何单词是否在描述中,如果是,请不要做任何事情。如果 my_list 不在描述中,我想返回字符串“找不到关键字”。

我将如何编写这段代码?

【问题讨论】:

标签: python


【解决方案1】:

您可以将all 与双重列表理解一起使用:

description = ['This is a random sentence. I like to travel and stuff','Hello world', 'Things on my bucket list, travel']
my_list = ['travel','fire']
def response():
   return "found" if any(i in b for i in my_list for b in description) else "Keywords not found"

【讨论】:

    【解决方案2】:

    将单词保存在集合中,并检查my_list 中的单词是否在集合中。这my_list 中没有短语时有效。即my_list 中的所有单词都是一个unigram。

    description = ['This is a random sentence. I like to travel and stuff','Hello world', 'Things on my bucket list, travel']
    my_list = ['travel','fire']
    set_words_in_description = set()
    for s in description:  
        # add new words in set_words_in_description
        set_words_in_description.update(set(w for w in s.split())) 
    

    使用isdisjoint

    def find_word_method_disjoint(my_list, set_words_in_description):
        # check if my_list is disjoint with set_wrods_in_description
        return not set_words_in_description.isdisjoint(my_list)
    
    %timeit find_word_method_disjoint(my_list, set_words_in_description)
    189 ns ± 1.53 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
    
    %timeit response()  # function given by the accepted answer.
    572 ns ± 9.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
    

    【讨论】:

      【解决方案3】:

      您可以使用re.findall()description中的句子中提取所有单词,并检查其中是否存在my_list中的任何单词:

      import re
      
      def find_words(words, text):
          desc_words = re.findall(r'[^\s,.]+', "".join(text))
      
          for word in words:
              if word in desc_words:
                  return "found"
      
          return "Keywords not found"
      

      哪些输出:

      >>> description = ['This is a random sentence. I like to travel and stuff','Hello world', 'Things on my bucket list, travel']
      >>> my_list = ['travel','fire']
      >>> find_words(my_list, description)
      found
      

      或者你可以使用set() 方法:

      def find_words(words, text):
          return "found" if set(words).intersection(re.findall(r'[^\s,.]+', "".join(text))) else "Keywords not found"
      

      注意:如果遇到除,. 以外的标点符号不同的句子,则必须更新正则表达式。

      【讨论】:

      • @AyodhyankitPaul 不幸的是。我不理解不发表评论的投票者。
      • 它发生了,别担心兄弟冷静,我们无法改变他们的行为:)
      • 是的,我对其他答案投了赞成票,因为将所有答案都投反对票看起来很愚蠢。
      【解决方案4】:

      你可以尝试设置这样的东西吗?

      description = ['This is a random sentence. I like to travel and stuff','Hello world', 'Things on my bucket list, travel']
      
      my_list = ['travel','fire']
      
      flat=[k for i in description for k in i.split()]
      
      
      print(any({i}.issubset(set(flat))for i in my_list))
      

      输出:

      True
      

      【讨论】:

        猜你喜欢
        • 2017-04-27
        • 1970-01-01
        • 1970-01-01
        • 2013-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-23
        • 2013-02-20
        相关资源
        最近更新 更多