title: Python: finding index of an array under several conditions
body: <p>I have the following problem. There are two n-dimensional arrays of integers and I need to determine the index of an item that fulfills several conditions. </p>
  
  <ul>
  <li>The index should have a negative element in "array1".</li>
  <li>Of this subset with negative elements, it should have the smallest value in "array2".</li>
  <li>In case of a tie, select the value that has the smallest value in "array1" (or the first otherwise)</li>
  </ul>
  
  <p>So suppose we have:</p>
  
  <pre><code>array1 = np.array([1,-1,-2])
  array2 = np.array([0,1,1])
  </code></pre>
  
  <p>Then it should return index 2 (the third number). I'm trying to program this as follows:</p>
  
  <pre><code>import numpy as np
  n = 3
  array1 = np.array([1,-1,-2])
  array2 = np.array([0,1,1])
  indices = [i for i in range(n) if array1[i]&lt;0] 
  indices2 = [i for i in indices if array2[i] == min(array2[indices])] 
  index = [i for i in indices2 if array1[i] == min(array1[indices2])][0] #[0] breaks the tie.
  </code></pre>
  
  <p>This seems to work, however, I don't find it very elegant. To me it seems like you should be able to do this in one or two lines and with defining less new variables. Anyone got a suggestion for improvement? Thanks in advance.</p>