Python - Select elements from matrix within range
<p>I have a question regarding python and selecting elements within a range.</p>

<p>If I have a n x m matrix with n row and m columns, I have a defined range for each column (so I have m min and max values).</p>

<p>Now I want to select those rows, where all values are within the range.</p>

<p>Looking at the following example:</p>

<pre><code>input = matrix([[1, 2], [3, 4],[5,6],[1,8]])
boundaries = matrix([[2,1],[8,5]])
#Note:
#col1min = 2
#col1max = 8
#col2min = 1
#col2max = 5
print(input)

desired_result = matrix([[3, 4]])
print(desired_result)
</code></pre>

<p>Here, 3 rows where discarded, because they contained values beyond the boundaries.</p>

<p>While I was able to get values within one range for a given array, I did not manage to solve this problem efficiently.</p>

<p>Thank you for your help.</p>

<p>I believe that there is more elegant solution, but i came to this:</p>

<pre><code>def foo(data, boundaries):
    zipped_bounds = list(zip(*boundaries))
    output = []
    for item in data:
        for index, bound in enumerate(zipped_bounds):
            if not (bound[0] &lt;= item[index] &lt;= bound[1]):
                break
        else:
            output.append(item)
    return output

data = [[1, 2], [3, 4], [5, 6], [1, 8]]
boundaries = [[2, 1], [8, 5]]
foo(data, boundaries)
</code></pre>

<p>Output:</p>

<pre><code>[[3, 4]]
</code></pre>

<p>And i know that there is not checking and raising exceptions if the sizes of arrays won't match each concrete size. I leave it OP to implement this.</p>

python