title: Finding indices of runs of integers in an array
body: <p>I have a numpy array with integers as entries. I need to find the indices corresponding to runs of integers in the array. For example if my array is <code>a = [1, 2, 3, 6, 7, 8]</code>, then the indices I want are <code>[[0, 1, 2], [3, 4, 5]]</code>. I have tried the following piece of code which uses a loop.</p>
  <pre><code>import numpy as np
  
  idx = np.array([0, 1, 2, 3, 8, 9, 10, 11, 12, 2, 3, 4, 5, 6, 55, 56, 89, 90, 91, 92])
  idx_cpy = idx
  
  indices = []
  
  while len(idx) &gt; 0:
      sorted_idx = np.arange(idx[0], idx.max()+1)[:len(idx)]
      bool_equal = np.equal(sorted_idx, idx)
      true_idx = np.argwhere(bool_equal == True)[:, 0]
      try:
          indices.append(np.array(indices[-1]).max() + 1 + true_idx)
      except IndexError:
          indices.append(true_idx)
      idx = idx[true_idx.max()+1:]
  
  
  &quot;&quot;&quot;
  indices = 
  [array([0, 1, 2, 3], dtype=int64), 
  array([4, 5, 6, 7, 8], dtype=int64), 
  array([ 9, 10, 11, 12, 13], dtype=int64), 
  array([14, 15], dtype=int64), 
  array([16, 17, 18, 19], dtype=int64)]
  &quot;&quot;&quot;
  </code></pre>
  <p>Although this works as expected, the variable <code>idx</code> in my actual code has large length and this takes a long time to complete. Is there a vectorized way of doing this? Thanks.</p>