我有一个字符串和一个单词列表,如果给定的文本字符串中存在这些单词,我想检查一下。我正在使用以下逻辑.....还有其他方法可以优化它:-
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")
最佳答案
使用set.issubset(other)
操作:
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 - 查找python文本中是否存在单词的逻辑,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57198547/