【发布时间】:2012-01-20 18:26:10
【问题描述】:
我有一个包含一些文本的 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)
还有很多代码,但这是我们测试 MyWords 中的所有单词是否都在 theTextContent 中的基本方法。在我看来,这太笨重了,不能成为好的 Python 代码
任何见解将不胜感激
【问题讨论】:
标签: python coding-style logic