Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.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_String_List_String Matching - Fatal编程技术网

Python字符串匹配-查找单词列表中的某个单词是否存在于另一个列表中的某个句子中

Python字符串匹配-查找单词列表中的某个单词是否存在于另一个列表中的某个句子中,python,string,list,string-matching,Python,String,List,String Matching,我有一个字符串和一个列表,定义如下 my_string = 'she said he replied' my_list = ['This is a cool sentence', 'This is another sentence','she said hello he replied goodbye', 'she replied', 'Some more sentences in here', 'et cetera et cetera...'] 我正在尝试检查my_string中的任何字符串

我有一个字符串和一个列表,定义如下

my_string = 'she said he replied'
my_list = ['This is a cool sentence', 'This is another sentence','she said hello he replied goodbye', 'she replied', 'Some more sentences in here', 'et cetera et cetera...']
我正在尝试检查
my_string
中的任何字符串中是否至少存在3个单词。我采用的方法是拆分
my_字符串
,然后使用
all
进行匹配。但是,只有当
my\u字符串
中的所有项目都存在于
my\u列表

if all(word in item for item in my_list for word in my_string.split()):
    print('we happy')
1-如果句子列表中至少有3项
my_string
,我如何使其满足条件


2-是否可以按照相同的顺序只匹配
my_string
中的第一个和最后一个单词?i、 e“她”和“回复”出现在
my_list
索引3的“她回复”中,返回True。

使用固有编码,即
True
为1,
False
为0。 对结果中的
值求和:

if sum(word in item for item in my_list for word in my_string.split()) >= 3:
    print('we happy')
对于您给定的输入,这将打印
we happy


关于:
mamun
的观点,我们还希望确保整个单词匹配。您需要在
my_list
中拆分每个字符串,以获得可用单词的列表
kaya3
已经发布了我要告诉您的操作。

使用固有的编码,即
True
为1,
False
为0。 对
结果中的
值求和:

if sum(word in item for item in my_list for word in my_string.split()) >= 3:
    print('we happy')
对于您给定的输入,这将打印
we happy


关于:
mamun
的观点,我们还希望确保整个单词匹配。您需要在
my_list
中拆分每个字符串,以获得可用单词的列表
kaya3
已经发布了我要告诉您的操作。

两个字符串之间的公共单词可以使用一个集合交集进行计算。结果集的
len
提供字符串共有的字数

首先使用集合并集在
my_list
中构建一组字符串中的所有单词:

all_words=set.union(*[set(item.split())用于我的_列表中的项])
然后检查交叉点的长度是否
=3

search\u words=set(my\u string.split())
如果len(搜索词和所有词)>=3:
打印(“我们快乐”)

两个字符串之间的公共单词可以使用集合交集进行计算。结果集的
len
提供字符串共有的字数

首先使用集合并集在
my_list
中构建一组字符串中的所有单词:

all_words=set.union(*[set(item.split())用于我的_列表中的项])
然后检查交叉点的长度是否
=3

search\u words=set(my\u string.split())
如果len(搜索词和所有词)>=3:
打印(“我们快乐”)

关于第1部分,我认为这应该行得通,我建议使用regex而不是string.split来查找单词。如果你的句子有复杂的单词和标点符号,你也可以使用nltk.word\u标记化。它们都比string.split慢,但如果需要,它们很有用

这里有几个不错的帖子强调了它们的区别(wordpunct tokenize基本上是一个伪装的单词regex):

导致

[假,假,真,假,假,假]

对于第2部分,类似的方法应该可以工作,尽管它不是一个超级干净的解决方案。如果您不希望它们只是按顺序排列,而是彼此相邻,请检查索引是否相隔1

words = get_words(my_string)
first_and_last = [words[0], words[-1]]
my_list_dicts = []
for sentence in my_list:
    word_dict = {}
    sentence_words = get_words(sentence)
    for i, word in enumerate(sentence_words):
        word_dict[word] = i
    my_list_dicts.append(word_dict)

result2 = []
for word_dict in my_list_dicts:
    if all(k in word_dict for k in first_and_last) and word_dict[first_and_last[0]] < word_dict[first_and_last[1]]:
        result2.append(True)
    else:
        result2.append(False)

print(result2)
words=获取单词(我的字符串)
第一个和最后一个=[单词[0],单词[-1]]
我的清单
对于我的清单中的句子:
单词_dict={}
句子单词=获取单词(句子)
对于i,枚举中的单词(句子中的单词):
单词dict[单词]=i
我的目录附加(单词目录)
结果2=[]
对于“我的目录”中的单词:
如果全部(第一个和最后一个单词中的k表示第一个和最后一个单词中的k)和第一个和最后一个单词[0]<第一个和最后一个单词[1]:
result2.append(True)
其他:
结果2.append(False)
打印(结果2)
结果:

[假,假,真,真,假,假]


关于第1部分,我认为这应该行得通,我建议使用regex而不是string.split来查找单词。如果你的句子有复杂的单词和标点符号,你也可以使用nltk.word_标记化。它们都比string.split慢,但如果需要,它们很有用

这里有几个不错的帖子强调了它们的区别(wordpunct tokenize基本上是一个伪装的单词regex):

导致

[假,假,真,假,假,假]

对于第2部分,类似的方法应该可以工作,尽管它不是一个超级干净的解决方案。如果您不希望它们只是按顺序排列,而是彼此相邻,请检查索引是否相隔1

words = get_words(my_string)
first_and_last = [words[0], words[-1]]
my_list_dicts = []
for sentence in my_list:
    word_dict = {}
    sentence_words = get_words(sentence)
    for i, word in enumerate(sentence_words):
        word_dict[word] = i
    my_list_dicts.append(word_dict)

result2 = []
for word_dict in my_list_dicts:
    if all(k in word_dict for k in first_and_last) and word_dict[first_and_last[0]] < word_dict[first_and_last[1]]:
        result2.append(True)
    else:
        result2.append(False)

print(result2)
words=获取单词(我的字符串)
第一个和最后一个=[单词[0],单词[-1]]
我的清单
对于我的清单中的句子:
单词_dict={}
句子单词=获取单词(句子)
对于i,枚举中的单词(句子中的单词):
单词dict[单词]=i
我的目录附加(单词目录)
结果2=[]
对于“我的目录”中的单词:
如果全部(第一个和最后一个单词中的k表示第一个和最后一个单词中的k)和第一个和最后一个单词[0]<第一个和最后一个单词[1]:
result2.append(True)
其他:
结果2.append(False)
打印(结果2)
结果:

[假,假,真,真,假,假]


您也可以使用flashtext来执行此操作

from flashtext import KeywordProcessor

kw_list = my_string.split()
kp = KeywordProcessor()
kp.add_keywords_from_list(kw_list) # add keyword that you are looking for 

def func_(x):
    kw = kp.extract_keywords(x)  # this will return all keyword present in the string
    return len(set(kw)) # now you find the sum of unique kw found in string 

print(list(map(func_, my_list)))
[0, 0, 4, 2, 0, 0]

您也可以使用flashtext来执行此操作

from flashtext import KeywordProcessor

kw_list = my_string.split()
kp = KeywordProcessor()
kp.add_keywords_from_list(kw_list) # add keyword that you are looking for 

def func_(x):
    kw = kp.extract_keywords(x)  # this will return all keyword present in the string
    return len(set(kw)) # now you find the sum of unique kw found in string 

print(list(map(func_, my_list)))
[0, 0, 4, 2, 0, 0]

对于“另一个”中的“他”它将返回True对于“另一个”中的“他”它将返回True