grouping a flat list of number by range

Jim Segrave jes at nl.demon.net
Thu Jun 1 19:15:17 EDT 2006


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)




More information about the Python-list mailing list