我有一个字符串和一个单词列表,我想检查它们是否存在于给定的文本字符串中。我正在使用下面的logic.....is来优化它:
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")发布于 2019-07-25 17:29:30
使用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")发布于 2019-07-25 17:30:20
你可以使用python的in运算符,我不知道它是不是更快。
str = "Messi is the best soccer player"
"soccer" in str
-> True
"football" in str
-> Falsehttps://stackoverflow.com/questions/57198547
复制相似问题