简体   繁体   English

如果字符串包含 python 中列表的所有单词,则匹配该字符串

[英]Matching a string if it contains all words of a list in python

I have a number of long strings and I want to match those that contain all words of a given list.我有许多长字符串,我想匹配那些包含给定列表中所有单词的字符串。

keywords=['special','dreams']
search_string1="This is something that manifests especially in dreams"
search_string2="This is something that manifests in special cases in dreams"

I want only search_string2 matched.我只想匹配 search_string2。 So far I have this code:到目前为止我有这段代码:

if all(x in search_text for x in keywords):
   print("matched")

The problem is that it will also match search_string1.问题是它也会匹配 search_string1。 Obviously I need to include some regex matching that uses \w or or \b, but I can't figure out how I can include a regex in the if all statement.显然我需要包含一些使用 \w 或 \b 的正则表达式匹配,但我不知道如何在if all语句中包含正则表达式。

Can anyone help?谁能帮忙?

you can use regex to do the same but I prefer to just use python.您可以使用正则表达式来做同样的事情,但我更喜欢只使用 python。

string classes in python can be split to list of words. python 中的字符串类可以拆分为单词列表。 (join can join a list to string). (加入可以加入一个列表到字符串)。 while using word in list_of_words will help you understand if word is in the list.word in list_of_words将帮助您了解单词是否在列表中。

keywords=['special','dreams']
found = True
for word in keywords:
    if not word in search_string1.split():
        found = False

Could be not the best idea, but we could check if one set is a part of another set:可能不是最好的主意,但我们可以检查一组是否是另一组的一部分:

keywords = ['special', 'dreams']

strs = [
  "This is something that manifests especially in dreams",
  "This is something that manifests in special cases in dreams"
]

_keywords = set(keywords)
for s in strs:
  s_set = set(s.split())
  if _keywords.issubset(s_set):
    print(f"Matched: {s}")

Axe319's comment works and is closest to my original question of how to solve the problem using regex. Axe319 的评论有效,并且最接近我最初提出的如何使用正则表达式解决问题的问题。 To quote the solution again:再次引用解决方案:

all(re.search(fr'\b{x}\b', search_text) for x in keywords)

Thanks to everyone!谢谢大家!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python检查字符串是否包含Python中的所有单词 - Python check if string contains all words in Python 检查一个字符串是否以相同的顺序包含另一个字符串的所有单词python? - Check if a string contains all words of another string in the same order python? 找出字符串是否不包含列表中的单词。 Python - Find out if string contains no words from a list. Python Python 检查字符串是否包含来自特定字符串列表的单词 - Python check if string contains words from specific list of strings 通过在 python 中传递字符串从列表中查找匹配的单词 - Find matching words from a list by passing string in python 在列表和字符串中查找匹配的单词 - Find matching words in a list and a string 删除python中字符串中2个匹配词之间的词 - Deletion of words between 2 matching words in a string in python Python 字符串匹配 - 查找单词列表中的特定数量的单词是否存在于另一个列表中的句子中 - Python string matching - Find if certain number of words in a list of words exist in a sentence in another list 如何在Python中没有子字符串匹配的情况下将2列表中的单词与另一个单词字符串匹配? - How to match words in 2 list against another string of words without sub-string matching in Python? Python:如果列表包含字符串打印列表中包含它的所有索引/元素 - Python: if list contains string print all the indexes / elements in the list that contain it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM