7

Is there a good way to find stretches of Trues in a numpy boolean array? If I have an array like:

x = numpy.array([True,True,False,True,True,False,False])

Can I get an array of indices like:

starts = [0,3]
ends = [1,4]

or any other appropriate way to store this information. I know this can be done with some complicated while loops, but I'm looking for a better way.

user3266890
  • 465
  • 5
  • 15

1 Answers1

11

You can pad x with Falses (one at the beginning and one at the end), and use np.diff. A "diff" of 1 means transition from False to True, and of -1 means transition from True to False.

The convention is to represent range's end as the index one after the last. This example complies with the convention (you can easily use ends-1 instead of ends to get the array in your question):

x1 = np.hstack([ [False], x, [False] ])  # padding
d = np.diff(x1.astype(int))
starts = np.where(d == 1)[0]
ends = np.where(d == -1)[0]
starts, ends
=> (array([0, 3]), array([2, 5]))
shx2
  • 61,779
  • 13
  • 130
  • 153