擅长:python、mysql、java
<p>你真的需要这里的<code>numpy</code>(如果不需要,请参阅<a href="https://stackoverflow.com/a/41980132/5393381">@Divakar's answer</a>)?如果这样做,可以将<code>list1</code>转换为<code>np.array</code>并索引:</p>
<pre><code>np.array(list1)[selection]
</code></pre>
<p>这被称为<a href="https://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#boolean-array-indexing" rel="nofollow noreferrer">^{<cd4>}</a>。以防你感兴趣。你知道吗</p>
<hr/>
<p>性能提示:如果您多次使用正则表达式:编译它并重用已编译的表达式:</p>
<pre><code>regexp = re.compile('[a-zA-Z]')
selection = np.array([bool(regexp.search(element)) for element in list1])
</code></pre>
<p>这可以更快更容易地与另一个答案结合起来。你知道吗</p>