title: Numpy array conditional matching
body: <p>I need to match two very large Numpy arrays (one is 20000 rows, another about 100000 rows) and I am trying to build a script to do it efficiently. Simple looping over the arrays is incredibly slow, can someone suggest a better way? Here is what I am trying to do: array <code>datesSecondDict</code> and array <code>pwfs2Dates</code> contain datetime values, I need to take each datetime value from array <code>pwfs2Dates</code> (smaller array) and see if there is a datetime value like that  (plus minus 5 minutes) in array <code>datesSecondDict</code> (there might be more than 1). If there is one (or more) I populate a new array (of the same size as array <code>pwfs2Dates</code>) with the value (one of the values) from array <code>valsSecondDict</code> (which is just the array with the corresponding numerical values to <code>datesSecondDict</code>). Here is a solution by @unutbu and @joaquin that worked for me (thanks guys!):</p>
  
  <pre><code>import time
  import datetime as dt
  import numpy as np
  
  def combineArs(dict1, dict2):
     """Combine data from 2 dictionaries into a list.
     dict1 contains primary data (e.g. seeing parameter).
     The function compares each timestamp in dict1 to dict2
     to see if there is a matching timestamp record(s)
     in dict2 (plus/minus 5 minutes).
     ==If yes: a list called data gets appended with the
     corresponding parameter value from dict2.
     (Note that if there are more than 1 record matching,
     the first occuring value gets appended to the list).
     ==If no: a list called data gets appended with 0."""
     # Specify the keys to use    
     pwfs2Key = 'pwfs2:dc:seeing'
     dimmKey = 'ws:seeFwhm'
  
     # Create an iterator for primary dict 
     datesPrimDictIter = iter(dict1[pwfs2Key]['datetimes'])
  
     # Take the first timestamp value in primary dict
     nextDatePrimDict = next(datesPrimDictIter)
  
     # Split the second dictionary into lists
     datesSecondDict = dict2[dimmKey]['datetime']
     valsSecondDict  = dict2[dimmKey]['values']
  
     # Define time window
     fiveMins = dt.timedelta(minutes = 5)
     data = []
     #st = time.time()
     for i, nextDateSecondDict in enumerate(datesSecondDict):
         try:
             while nextDatePrimDict &lt; nextDateSecondDict - fiveMins:
                 # If there is no match: append zero and move on
                 data.append(0)
                 nextDatePrimDict = next(datesPrimDictIter)
             while nextDatePrimDict &lt; nextDateSecondDict + fiveMins:
                 # If there is a match: append the value of second dict
                 data.append(valsSecondDict[i])
                 nextDatePrimDict = next(datesPrimDictIter)
         except StopIteration:
             break
     data = np.array(data)   
     #st = time.time() - st    
     return data
  </code></pre>
  
  <p>Thanks,
  Aina.</p>