Break up list into groups
danmcleran at yahoo.com
danmcleran at yahoo.com
Mon Jul 16 18:53:55 EDT 2007
> Here's an ugly way I wouldn't recommended (using itertools groupby):
>
> py> from itertools import groupby
> py> alist = [0xF0, 1, 2, 3, 0xF0, 4, 5, 6,
> ... 0xF1, 7, 8, 0xF2, 9, 10, 11, 12, 13,
> ... 0xF0, 14, 0xF1, 15]
> py> def doit(alist):
> ... i = (list(g) for k,g in groupby(alist, lambda x: 0xf0&x))
> ... return [k for k in [j + i.next() for j in i] if len(k)>1]
> ...
> py> doit(alist)
>
> [[240, 1, 2, 3],
> [240, 4, 5, 6],
> [241, 7, 8],
> [242, 9, 10, 11, 12, 13],
> [240, 14],
> [241, 15]]
>
> James
Wow that is ugly. I think I like the simpler way better.
Thanks
More information about the Python-list
mailing list