Quickly filter elements in Python array by minimum and maximum values
<p>I have a somewhat large <code>numpy</code> array of floats (<code>large_array</code>, ~2e7 elements). I need to generate a new array, filtering out all elements beyond certain minimum and maximum values.</p>

<p>I can do this with a simple:</p>

<pre><code>import numpy as np
large_array = np.random.uniform(0., 10000., 20000000)
min_val, max_val = 500., 2700.
arr_f = []
for _ in large_array:
    if min_val &lt;= _ &lt;= max_val:
        arr_f.append(_)
</code></pre>

<p>but it takes a really long time.</p>

<p>How can I speed this up?</p>

<p>try this:</p>

<pre><code>In [18]: large_array.shape
Out[18]: (20000000,)

In [26]: new = large_array[(large_array &gt;= min_val) &amp; (large_array &lt;= max_val)]

In [27]: new
Out[27]: array([  814.24315891,  1611.53346093,   624.31833231, ...,  1999.08383068,  2212.9825087 ,  1786.08963269])

In [28]: new.shape
Out[28]: (4400475,)
</code></pre>

python arrays numpy