擅长:python、mysql、java
<p>可以使用<code>any()</code>和<code>in</code>运算符:</p>
<pre><code>lines = """
rumpelstiltskin foo bar
hansel rumpelstiltskin
gretchel bar
hansel foo
""".splitlines()
seek = ['foo', 'bar']
for line in lines:
if any(word in line for word in seek):
print line
print [line for line in lines if any(word in line for word in seek)]
</code></pre>
<p>输出:</p>
<pre><code>rumpelstiltskin foo bar
gretchel bar
hansel foo
['rumpelstiltskin foo bar', 'gretchel bar', 'hansel foo']
</code></pre>