擅长:python、mysql、java
<p>使用内置的<code>all()</code>可以将多个布尔表达式或函数放在一个iterable中并坚持理解。我认为这是一个相当少的使用内置,它保持可读性高。</p>
<pre><code>>>> [x for x in range(20) if all([1 < x < 10, not x & 1])]
[2, 4, 6, 8]
</code></pre>
<p>或者</p>
<pre><code>>>> [x for x in range(20) if all([foo(x), bar(x)])]
</code></pre>
<p>如果只需要满足一个条件,内置的<code>any()</code>也可以在这里正常工作:</p>
<pre><code>>>> [x for x in range(20) if any([1 < x < 10, not x & 1])]
[0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18]
</code></pre>