查找python文本中是否存在单词的逻辑
我有一个字符串和一个单词列表,我想检查它们是否存在于给定的文本字符串中。我正在使用以下逻辑…是否有其他方法来优化它:-查找python文本中是否存在单词的逻辑,python,pandas,python-2.7,text,nlp,Python,Pandas,Python 2.7,Text,Nlp,我有一个字符串和一个单词列表,我想检查它们是否存在于给定的文本字符串中。我正在使用以下逻辑…是否有其他方法来优化它:- import re text=""" Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built in data structures, combined with dynamic typing an
import re
text="""
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.
Its high-level built in data structures, combined with dynamic typing and dynamic binding, make
it very attractive for Rapid Application Development"""
tokens_text=re.split(" ",text)
list_words=["programming","Application"]
if (len(set(list_words).intersection(set(tokens_text)))==len(list_words)):
print("Match_Found")
操作:
text="""
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics.
Its high-level built in data structures, combined with dynamic typing and dynamic binding, make
it very attractive for Rapid Application Development"""
tokens = text.split()
list_words = ["programming", "Application"]
if (set(list_words).issubset(set(tokens))):
print("Match_Found")
或者只需使用all
功能:
if all(x in tokens for x in list_words):
print("Match_Found")
您可以使用python的in操作符,我不知道它是否更快
str = "Messi is the best soccer player"
"soccer" in str
-> True
"football" in str
-> False
这将需要更多的时间,因为它将在for循环中的每一个元素。而且我想检查一下,如果列表中的这两个词都出现在文本中。我认为在这种情况下它不会有帮助……是的,你也应该循环搜索字符串,祝你好运:)