grouping a flat list of number by range
Paddy
paddy3118 at netscape.net
Fri Jun 2 02:18:26 EDT 2006
Jim Segrave wrote:
> In article <1149202780.780252.242110 at f6g2000cwb.googlegroups.com>,
> Paddy <paddy3118 at netscape.net> wrote:
> >
> >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]]
>
> Fails on an empty list, as tmp is not defined when it hits the yield
>
>
> --
> Jim Segrave (jes at jes-2.demon.nl)
Yep, I still, purposfully, decided not to put any guard checks in
because:
Its very easy to add.
It detracts from the algorithm.
It might never be called with an empty list in-situ.
It is correct for the original posters testcases.
Yah gotta leave-em something to do ;-)
But you-too are right. Don't just cut-n-paste newsgroup solutions. they
need testing for your particular use. I doubt anyone on C.L.P. would be
malicious, but your actual use of a function having a wider scope to
inputs than mentioned might be fairy common.
(Damn, I wanted to use 'caveat emptor', but it does not apply as
nothing is being bought. Oh well :-)
-- Pad.
More information about the Python-list
mailing list