Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>According to the first sentence in your question, you want to select values based on the values in another list or array.</p> <p>In numpy you can use indexing to get selected values from an array. I use <a href="http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#boolean" rel="nofollow">Boolean indexing</a> in the example. This avoids the need to append values to an existing array, but gives you a copy of the selected values as an array. You can combine multiple conditions using the <code>&amp;</code> or <code>|</code> operator, <a href="http://docs.scipy.org/doc/numpy/reference/routines.logic.html" rel="nofollow">logic functions</a> from numpy or your own functions. </p> <pre><code>In [1]: import numpy as np In [2]: size = int(1E7) In [3]: ar = np.arange(size) In [4]: ar2 = np.random.randint(100, size=size) In [5]: %timeit ar[(ar2 &gt; 50) &amp; (ar2 &lt; 70) | (ar2 == 42)] 10 loops, best of 3: 249 ms per loop </code></pre> <p>If you need every selection in a separate array based on different conditions (or ranges as given in the comment) you can do something like this:</p> <pre><code>conditions = [(10, 20), (20, 50)] # min, max as tuples in a list results = {} for condition in conditions: selection = ar[(ar2 &gt; condition[0]) &amp; (ar2 &lt; condition[1])] # do something with the selection ? results[condition] = selection print results </code></pre> <p>will give you something like that</p> <pre><code>{(20, 50): array([ 2, 6, 7, ..., 9999993, 9999997, 9999998]), (10, 20): array([ 1, 3, 66, ..., 9999961, 9999980, 9999999])} </code></pre> <p>You should avoid looping over a numpy array in general, but instead use vectorized functions to manipulate your arrays. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload