Break up list into groups
Ron Adam
rrr at ronadam.com
Fri Jul 20 11:35:51 EDT 2007
Matimus wrote:
> Excellent work! One more modification and I get below 10us/pass:
>
> def getgroups(seq):
> groups = []
> push = groups.append
> iseq = iter(xrange(len(seq)))
> for start in iseq:
> if seq[start] & 0x80:
> for stop in iseq:
> if seq[stop] & 0x80:
> push(seq[start:stop])
> start = stop
> push(seq[start:])
> return groups
>
> -Matt
Looks good to me. :-)
So a generator versions would be...
(not tested)
def getgroups(seq):
iseq = iter(xrange(len(seq)))
for start in iseq:
if seq[start] & 0x80:
for stop in iseq:
if seq[stop] & 0x80:
yield seq[start:stop]
start = stop
yield seq[start:]
(I also wanted to compare this to Georges solution, maybe later.)
Now if there is some way to generalize this so it can be used in a broader
range of situations without loosing too much of it's efficiency. Of course
then maybe group by would be better. <shrug>
Cheers,
Ron
More information about the Python-list
mailing list