title: Is there a way to understand clusters of zeros and then remove it from numpy array?
body: <p>I want to remove zeros from the middle of a numpy array (but not all zeros should be removed)</p>
  
  <p>Removing zeros are demonstrated in multiple examples across stackoverflow, but I am still finding it hard to write a logic for my problem.</p>
  
  <pre><code>import numpy as np
  
  a = np.array([255,255,255,255,255
  ,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
  ,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
  ,255,255,255,255,255,255,255,255,255,255,255,255,255,0,255,255,255,255
  ,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
  ,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,207,0,0
  ,159,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
  ,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
  ,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
  ,255,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
  ,0,0,0,0,0,0,0,0,0,88,239,255,255,255,255,255,255,255
  ,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,231,88])
  </code></pre>
  
  <p>I have an array a, where there are some non-zero values and a big cluster of zeros (along with some zeros in the middle of the non-zero values). I want to remove that big cluster of zeros or find the index where that big cluster starts. Then reduce the array to the following form:</p>
  
  <pre><code>a1 = [255,255,255,255,255
  ,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
  ,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
  ,255,255,255,255,255,255,255,255,255,255,255,255,255,0,255,255,255,255
  ,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
  ,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,207,0,0
  ,159,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
  ,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
  ,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255
  ,255,64]
  </code></pre>
  
  <p>and</p>
  
  <pre><code>a2=[88,239,255,255,255,255,255,255,255
  ,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,231,88]
  </code></pre>
  
  <p>Notice that the array a1, still has some zeros in it. the operation should only remove the zeros if there are a certain number of consecutive zeros in it(say 10 zeros). I can do it with loops iterating over the array, but if a simpler and faster approach can be suggested, that would be great.</p>