Re: Chunking sequential values in a list

2006-07-14 Thread bearophileHUGS
Peter Otten: > which is almost identical to the last example in > http://docs.python.org/lib/itertools-example.html I see, thank you. I haven't had enoug time and brain to fix it (and the OP question seemed like homework, so leaving some other things to do is positive). I think still that too muc

Re: Chunking sequential values in a list

2006-07-14 Thread Peter Otten
[EMAIL PROTECTED] wrote: > If you want something less easy to understand, you can try fixing the > following code that doesn't work: > > from itertools import groupby > l = [1,2,3,5,6,8,12] > keyf = lambda (pos,x): x-l[pos]>1 > [[el[1] for el in gr] for he,gr in groupby(enumerate(l[:-1]), keyf)]

Re: Chunking sequential values in a list

2006-07-14 Thread Gerard Flanagan
David Hirschfield wrote: > I have this function: > > def sequentialChunks(l, stride=1): > chunks = [] > chunk = [] > for i,v in enumerate(l[:-1]): > v2 = l[i+1] > if v2-v == stride: > if not chunk: > chunk.append(v) > chunk.append

Re: Chunking sequential values in a list

2006-07-14 Thread Gerard Flanagan
[EMAIL PROTECTED] wrote: > Gerard> David Hirschfield wrote: > >> I have this function: > >> > >> def sequentialChunks(l, stride=1): > ... > >> > >> Which takes a list of numerical values "l" and splits it into chunks > >> where each chunk is sequential... > > Gerard

Re: Chunking sequential values in a list

2006-07-13 Thread Paul Rubin
David Hirschfield <[EMAIL PROTECTED]> writes: > So sequentialChunks([1,2,3,5,6,8,12]) returns: > [[1,2,3],[5,6],[8],[12]] Ugly and not too efficient: find the break points and use them to make sub-lists. def sequentialChunks(l, stride=1): p = [0] + [i for i in xrange(1,len(l)) if l[i]-l[i-1]

Re: Chunking sequential values in a list

2006-07-13 Thread skip
Gerard> David Hirschfield wrote: >> I have this function: >> >> def sequentialChunks(l, stride=1): ... >> >> Which takes a list of numerical values "l" and splits it into chunks >> where each chunk is sequential... Gerard> see the groupby example here: G

Re: Chunking sequential values in a list

2006-07-13 Thread Gerard Flanagan
David Hirschfield wrote: > I have this function: > > def sequentialChunks(l, stride=1): > chunks = [] > chunk = [] > for i,v in enumerate(l[:-1]): > v2 = l[i+1] > if v2-v == stride: > if not chunk: > chunk.append(v) > chunk.append

Re: Chunking sequential values in a list

2006-07-13 Thread bearophileHUGS
It looks like homework. Sometimes the simpler code is better: def splitter(seq): if not seq: return [] result = [] current = [seq[0]] for pos, el in enumerate(seq[1:]): if el - current[-1] > 1: result.append(current[:]) current = [] c

Chunking sequential values in a list

2006-07-13 Thread David Hirschfield
I have this function: def sequentialChunks(l, stride=1): chunks = [] chunk = [] for i,v in enumerate(l[:-1]): v2 = l[i+1] if v2-v == stride: if not chunk: chunk.append(v) chunk.append(v2) else: if not chunk: