Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何检查字符串列表是否在另一个字符串列表中?_Python - Fatal编程技术网

Python 如何检查字符串列表是否在另一个字符串列表中?

Python 如何检查字符串列表是否在另一个字符串列表中?,python,Python,范例 我想检查一下我清单上的单词是否在描述中,如果是,请不要做任何事情。如果我的列表不在描述中,我希望返回字符串“Keywords not found” 我该如何编写此代码?您可以使用双列表理解的all: description = ['This is a random sentence. I like to travel and stuff','Hello world', 'Things on my bucket list, travel'] my_list = ['travel','fir

范例

我想检查一下我清单上的单词是否在描述中,如果是,请不要做任何事情。如果我的列表不在描述中,我希望返回字符串“Keywords not found”


我该如何编写此代码?

您可以使用双列表理解的
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']
您可以使用从
description
中的句子中提取所有单词,并检查其中是否存在
my_list
中的任何单词:

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"
哪些产出:

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"
或者您可以使用此
set()
方法:

>>> 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

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

将单词保存在集合中,并检查
my_list
中的单词是否在集合中。只有当
我的列表中没有短语时,此才起作用。i、 e.
my_list
中的所有单词都是单字

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

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())) 

你可以试试这样的设置吗

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)
输出:

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))

“通常的方式”对你有帮助吗?你在
my_list
中的单词是否包含短语?可能重复的可能重复的可能重复的@ayodhyankittpaul是的。我不理解那些不发表评论的反对者。事实上,不用担心,兄弟,我们不能改变他们的行为:)是的,我对其他答案投了高票,因为把所有答案都投了反对票看起来很愚蠢。
True