擅长:python、mysql、java
<p>您可以使用list comp,使用<a href="https://docs.python.org/2/library/functions.html#all" rel="nofollow">all</a>查找包含所有偶数的数字:</p>
<pre><code>print([s for s in numbers if all(not int(ch) % 2 for ch in s)])
</code></pre>
<p><code>all</code>查找任何奇数都会短路。</p>
<p>如果不想一次将所有数字存储在内存中,可以使用<a href="https://docs.python.org/2/reference/expressions.html#generator-expressions" rel="nofollow">generator expression</a>:</p>
<pre><code>evens = (s for s in numbers if all(not int(ch) % 2 for ch in s))
</code></pre>
<p>要访问数字,您只需在晚上重复:</p>
<pre><code>for n in evens:
print(n)
</code></pre>
<p>还可以对函数方法使用filter,该方法在python 3中返回迭代器:</p>
<pre><code>In [5]: evens = filter(lambda x: all(not int(ch) % 2 for ch in x), numbers)
In [6]: next(evens)
Out[6]: '0'
In [7]: next(evens)
Out[7]: '2'
In [8]: next(evens)
Out[8]: '4'
In [9]: next(evens)
Out[9]: '6'
</code></pre>