[Numpy-discussion] ‎ Find contiguous sequences of booleans in arrays

David Cournapeau david@ar.media.kyoto-u.ac...
Mon Sep 17 05:33:03 CDT 2007

Kurdt Bane wrote:
> Hi to all,
> I've got an 1-D array of bools and I'd like to find the length of the 
> first contiguous sequence of True values, starting from position [0] 
> of the array.
> (That's equivalent to find the position of the first occurrence of 
> False in the array).
One possibility would be to find the first True and then use cumproduct 
to get the length, and then create a view with the remaining elements, 
recursively. But this may be slow. You may be a bit smarter to get the 
index of the first True value for each sub-sequence by multiplying the 
array by itself, shifted by one, and using xor. Whether this is pythonic 
and clean, I don't know :)



More information about the Numpy-discussion mailing list