grouping a flat list of number by range
Paddy
paddy3118 at netscape.net
Thu Jun 1 18:59:40 EDT 2006
Jim Segrave wrote:
> In article <1149201416.196975.123370 at c74g2000cwc.googlegroups.com>,
> Paddy <paddy3118 at netscape.net> wrote:
> >=== interv2 ===
> >>>> def interv2(inlist):
> >... for i,val in enumerate(inlist):
> >... if i==0:
> >... tmp = val
> >... elif val != valinc:
> >... yield [tmp, valinc]
> >... tmp = val
> >... valinc = val+1
> >... yield [tmp, valinc]
> >...
> >>>> list(interv2(inlist))
> >[[3, 4], [6, 9], [12, 14], [15, 16]]
> >
> >=== END interv2 ===
>
> This doesn't actually run, changing it to make it do so:
>
> def interv2(inlist):
> tmp = valinc = 0
> for i,val in enumerate(inlist):
> if i==0:
> tmp = val
> valinc = val + 1
> elif val != valinc:
> yield [tmp, valinc]
> tmp = val
> valinc = val+1
> yield [tmp, valinc]
>
> it now works, but returns [0, 0] when passed an empty list, when it
> should return nothing at all
> --
> Jim Segrave (jes at jes-2.demon.nl)
Jim, I had tabs/spaces indent problems when cut-n-pasting.
What I ran was more like the version below, but i did a quick
separation of the line that has the ';' in it and goofed.
>>> def interv2(inlist):
... for i,val in enumerate(inlist):
... if i==0:
... tmp = val
... elif val != valinc:
... yield [tmp, valinc]; tmp = val
... valinc = val+1
... yield [tmp, valinc]
...
>>> list(interv2(inlist))
[[3, 4], [6, 9], [12, 14], [15, 16]]
More information about the Python-list
mailing list