<p>在文件中有下面的行</p>
<pre><code>god is great
god is excellent
i am excellent
you are not good
I am also bad
world is awesome
</code></pre>
<p>在所有这些行中,我需要填充下面三行,但这些行不应该与“good”和“awesome”这样的词相匹配</p>
<p>这是密码。但它并没有像预期的那样起作用</p>
<pre><code>f = open('test2.txt')
lines = [line for line in f.readlines() if line.strip()]
total_lines=len(lines)
index1=[0,1,2]
all_index=list(range(0,total_lines))
list3 = [i for i in all_index if not i in index1] ## Capturing Index (3,4,5)
words_not_require=['good','awesome'] ## Lines matching these words not required in final list
for j in list3:
print (''.join(i for i in lines[j] if i not in words_not_require))
</code></pre>
<p>我想你应该把最后两行改成:</p>
<pre><code>for j in list3:
print(' '.join([word for word in lines[j].split() if word not in words_not_require]))
</code></pre>
<p>输出:</p>
<pre><code>you are not
I am also bad
world is
</code></pre>