title: How do I find x given y or vice versa in numpy?
body: <p>For example, I have the following arrays:</p>
  
  <pre><code>x = [0, 1, 2, 3, 4.5, 5]
  y = [2, 8, 3, 7,   8, 1]
  </code></pre>
  
  <p>I would like to be able to do the following given <code>x</code>:</p>
  
  <pre><code>&gt;&gt;&gt; what_is_y_when_x_is(2)
  (2, 3)
  &gt;&gt;&gt; what_is_y_when_x_is(3.1) # Perhaps set rules to round to nearest (or up or down)
  (3, 7)
  </code></pre>
  
  <p>On the other hand, when given <code>y</code>:</p>
  
  <pre><code>&gt;&gt;&gt; what_is_x_when_y_is(2)
  (0, 2)
  &gt;&gt;&gt; what_is_x_when_y_is(max(y))
  ([1, 4.5], 8)
  </code></pre>
  
  <h2>The circumstances of this problem</h2>
  
  <p>I could have plotted <code>y</code> versus <code>x</code> using a closed analytical function, which should be very easy by just calling <code>foo_function(x)</code>. However, I'm running <strong>numerical simulations</strong> whose data plots do not have closed analytical solutions.</p>
  
  <h2>Attempted solution</h2>
  
  <p>I've tackled similar problems before and approached them roughly this way:</p>
  
  <p><code>what_is_y_when_x_is(some_x)</code></p>
  
  <ol>
  <li>Search the array <code>x</code> for <code>some_x</code>.</li>
  <li>Get its index, <code>i</code>.</li>
  <li>Pick up <code>y[i]</code>.</li>
  </ol>
  
  <h2>Question</h2>
  
  <p>Is there a better way to do this? Perhaps a built-in <code>numpy</code> function or a better algorithm?</p>