efficiently finding the interval with non-zeros in scipy/numpy in Python?

Posted by user248237 on Stack Overflow See other posts from Stack Overflow or by user248237
Published on 2010-04-12T01:25:29Z Indexed on 2010/04/12 1:33 UTC
Read the original article Hit count: 225

Filed under:
|
|
|

suppose I have a python list or a python 1-d array (represented in numpy). assume that there is a contiguous stretch of elements how can I find the start and end coordinates (i.e. indices) of the stretch of non-zeros in this list or array? for example,

a = [0, 0, 0, 0, 1, 2, 3, 4]

nonzero_coords(a) should return [4, 7]. for:

b = [1, 2, 3, 4, 0, 0]

nonzero_coords(b) should return [0, 2].

thanks.

© Stack Overflow or respective owner

Related posts about python

Related posts about scipy