Contiguous subsequences? Paging Dr. Groupby, Dr. itertools.groupby:
>>> from itertools import groupby
>>> l = [240,200,160,4,0,0,0,0,4,4,4,0,0,0,1,1,1,1]
>>> [list(g) for k,g in groupby(l, lambda x: x != 0) if k]
[[240, 200, 160, 4], [4, 4, 4], [1, 1, 1, 1]]
or even if we take advantage of the fact bool(0)
is False
and bool(any other integer)
is True
:
>>> [list(g) for k,g in groupby(l, bool) if k]
[[240, 200, 160, 4], [4, 4, 4], [1, 1, 1, 1]]