[SciPy-user] indices of consecutive elements

Pierre GM pgmdevlist@gmail....
Tue Dec 2 13:10:20 CST 2008


On Dec 2, 2008, at 1:37 PM, Daniel Ashbrook wrote:

> Wow, ask and ye shall receive way more than you expected! Thanks so
> much, Pierre - it's what I need:
>
> a=[0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1,  
> 0]
> c = Cluster(a,0)
> z = zip(list(c.starts), [list(i) for i in c.clustered])
> r = [(i,i+len(j)-1) for i,j in z if j[0] == 1 and len(j) >= 4]
> print(r)
>
> [(2, 5), (15, 18)]


There's simpler:
 >>> [(_.start,_.stop-1) for _ in  c.slices[(c.sizes>=4) &  
(c.uniques==1)]]

as c.slices is an array, you can directly select its elements that  
satisfy some conditions: here, we take the clusters that cover at  
least 4 element, provided the unique element is 1...


More information about the SciPy-user mailing list