Find all occurences of a specified match of two numbers in numpy array
<p>what i need to achieve is to get array of all indexes, where in my data array filled with zeros and ones is step from zero to one. I need very quick solution, because i have to work with milions of arrays of hundrets milions length. It will be running in computing centre. For instance..</p>

<pre><code>data_array = np.array([1,1,0,1,1,1,0,0,0,1,1,1,0,1,1,0])
result = [3,9,13]
</code></pre>

<p>Since it's an array filled with <code>0s</code> and <code>1s</code>, you can benefit from just comparing rather than performing arithmetic operation between the one-shifted versions to directly give us the boolean array, which could be fed to <code>np.flatnonzero</code> to get us the indices and the final output. </p>

<p>Thus, we would have an implementation like so -</p>

<pre><code>np.flatnonzero(data_array[1:] &gt; data_array[:-1])+1
</code></pre>

<p>Runtime test -</p>

<pre><code>In [26]: a = np.random.choice([0,1], 10**8)

In [27]: %timeit np.nonzero((a[1:] - a[:-1]) == 1)[0] + 1
1 loop, best of 3: 1.91 s per loop

In [28]: %timeit np.where(np.diff(a)==1)[0] + 1
1 loop, best of 3: 1.91 s per loop

In [29]: %timeit np.flatnonzero(a[1:] &gt; a[:-1])+1
1 loop, best of 3: 954 ms per loop
</code></pre>

python arrays performance numpy