[Numpy-discussion] List/location of consecutive integers

Christopher Barker Chris.Barker at noaa.gov
Fri May 22 18:13:00 EDT 2009


David Warde-Farley wrote:
> I don't think this is very general:
> 
> In [53]: indices
> Out[53]:
> array([   -3,     1,     2,     3,     4,     5,     6,     7,     8,
>             9,   255,   256,   257,   258, 10001, 10002, 10003, 10004])
> 
> In [54]: breaks = diff(indices) != 1
> 
> In [55]: zip(indices[np.r_[True, breaks[:-1]]], indices[breaks])
> Out[55]: [(-3, -3), (1, 9), (255, 258)]

that's why I put a sys.maxint at the end of the series...


In [13]: indices = np.array([   -3,     1,     2,     3,     4,     5, 
    6,     7,     8,
             9,   255,   256,   257,   258, 10001, 10002, 10003, 10004, 
sys.maxint])

In [15]: breaks = np.diff(indices) != 1

In [16]: zip(indices[np.r_[True, breaks[:-1]]], indices[breaks])
Out[16]: [(-3, -3), (1, 9), (255, 258), (10001, 10004)]


Though that's probably not very robust!

-Chris



-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov



More information about the NumPy-Discussion mailing list