PyN*_*bie 1 python logic coding-style
我有一个包含一些文本的html元素列表.我需要找到包含我要提供的所有单词的元素.我有一些代码完成了我想要的,但我相信有更好的方法来做到这一点
myWords=['some', 'supplied','words']
theTextContents='a string that might or might not have all of some supplied words'
goodElements=[]
count=0
for word in myWords:
if word in TheTextContents:
count+=1
if count==len(myWords):
goodElements.append(theTextContents)
Run Code Online (Sandbox Code Playgroud)
还有更多代码,但这是我们测试的基本方法,以查看MyWords中的所有单词是否都在theTextContent中.在我看来,这太笨重了,不能成为优秀的Python代码
任何见解将不胜感激
if all(word in theTextContents.split() for word in myWords):
...
Run Code Online (Sandbox Code Playgroud)
all Python 2.5+中的函数