Filter Multi-Dimensional Array
<p>I have an array (lists) which is NxK. However, I want to "filter" is after inputting some constraints based on values in Columns 4 and 6. This is the code I have so far.</p>

<pre><code>minmag = 5
maxmag = 7

mindist = 25
maxdist = 64

filter = np.zeros((1, 7), dtype='object')
add = np.zeros((1, 7), dtype='object')
k = 0

for i in range(0,len(lists)):
    if lists[i, 4]&gt;= minmag and lists [i, 4] &lt;= maxmag and lists [i, 6]&gt;=mindist and  lists [i, 6]&lt;= maxdist:
        if k == 0:
            for x in range(0,16):
                filter[0, x] = lists[i, x]
            k = 1
        else:
            for x in range(0, 16):
                add[0, x] = lists[i, x]
            filter = np.append(filter, add, axis=0)
</code></pre>

<p>It works, however it is not so neat. Just wondering if anyone has a better solution.</p>

<p>Simplifying the most repetitive parts:</p>

<pre><code>if k==0:
    for x in xrange(1,8):
        lists[i,x] = filter[0,x]
    k = 1
else:
    for x in xrange(1,8):
        lists[i,x] = add[0,x]
    filter = np.append(filter, add, axis=0)
</code></pre>

<p>You could also combine your nested <code>if</code>s into a single one with the 4 conditions combined with <code>and</code>s.</p>

<p>I also believe (not seeing how <code>lists</code> is defined, I'm not sure) you can replace the outer loop with</p>

<pre><code>for row in lists:
</code></pre>

<p>and then use <code>row[x]</code> in place of <code>lists[i,x]</code></p>

python arrays filter