[Numpy-discussion] List/location of consecutive integers
josef.pktd at gmail.com
josef.pktd at gmail.com
Fri May 22 16:27:38 EDT 2009
On Fri, May 22, 2009 at 3:59 PM, David Warde-Farley <dwf at cs.toronto.edu> wrote:
> On 22-May-09, at 1:03 PM, Christopher Barker wrote:
>
>> In [104]: zip(indices[np.r_[True, breaks[:-1]]], indices[breaks])
>
>
>
> 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)]
>
this still works:
>>> indices = np.array([-5,-4,-3,1,1,2,3,4,5,6,7,8,9,255,256,257,258,10001,10002,10003,10004])
>>> idx = (np.diff(indices) != 1).nonzero()[0]
>>> idxf = np.hstack((-1,idx,len(indices)-1))
>>> vmin = indices[idxf[:-1]+1]
>>> vmax = indices[idxf[1:]]
>>> zip(vmin,vmax)
[(-5, -3), (1, 1), (1, 9), (255, 258), (10001, 10004)]
Josef
More information about the NumPy-Discussion
mailing list