<p>这个</p>
<pre><code>if badWord in txtEntry:
</code></pre>
<p>测试<code>badWord</code>是否等于<code>textEntry</code>中的任何子字符串。因为它是一个列表,所以它没有也不能-您需要做的是分别检查badWord中的每个字符串。最简单的方法是使用函数<code>any</code>。不过,您确实需要将<code>txtEntry</code>标准化,因为(如注释中所述)您关心的是匹配精确的单词,而不仅仅是子字符串(这些子字符串是<code>string in string</code>测试的),而且您(可能)希望搜索不区分大小写:</p>
<pre><code>import re
for txtEntry in txtList:
# Ensure that `word in contents` doesn't give
# false positives for substrings - avoid eg, 'ass in class'
contents = [w.lower() for w in re.split('\W+', txtEntry)]
if any(word in contents for word in badWord):
myClassifier.append('bad')
else:
myClassifer.append('good')
</code></pre>
<p>请注意,与其他答案一样,我使用了<code>list.append</code>方法而不是<code>+=</code>将字符串添加到列表中。如果您使用<code>+=</code>,您的列表将变成这样:<code>['g', 'o', 'o', 'd', 'b', 'a', 'd']</code>而不是<code>['good', 'bad']</code>。你知道吗</p>
<p>根据对问题的评论,如果您想在仅存储文件名时检查文件的<em>内容</em>,则需要稍微调整此项—您需要调用<code>open</code>,然后需要对内容进行测试—但测试和规范化保持不变:</p>
<pre><code>import re
for txtEntry in txtList:
with open(txtEntry) as f:
# Ensure that `word in contents` doesn't give
# false positives for substrings - avoid eg, 'ass in class'
contents = [w.lower() for w in re.split('\W+', f.read())]
if any(word in contents for word in badWord):
myClassifier.append('bad')
else:
myClassifer.append('good')
</code></pre>
<p>这些循环都假设,与示例数据一样,badWord中的所有字符串都是小写的。你知道吗</p>