0

Say you have a boolean/binary array in Numpy called 'a', where 'a' is a sequence of 0s and 1s (or equivalently True/False). I want to find the distances between the 1s in 'a'.

Eg. a = [1,**0,0**,1,**0**,1,**0,0,0,0,0**,1]. Output = [2,1,5]

What is the most efficient way to evaluate this in Numpy Python? The actual dataset is of the order of 1,000,000 binary values.

Premkumar chalmeti
  • 800
  • 1
  • 8
  • 23

2 Answers2

3

Here is a numpy way of getting the result:

np.diff(np.where(np.array(a)>0))-1
Han Wang
  • 162
  • 8
2
a = [1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1]
output = []
i = 0
space = 0
for ii in a:
  if a[i] == 1:
    output.append(space)
    space = 0
  elif a[i] == 0:
    space += 1
  i += 1
print(output)

This gives a zero at the beginning, but otherwise is exactly what you want.

PythonNerd
  • 293
  • 1
  • 11