my language is python
the input looks something like:
'0 0 0 0 1 0 0 0 1 1 0 0'
and so on.
i want the output:
('0 0 0 0', '1 0 0 0', '1 1 0 0')
or every set of 4 numbers in is its own element
so far i have put together
>>> truth = re.compile('(([0-1]\D*?){4})*')
>>> truth.search('0 0 0 0 1 0 0 0').groups()
('0 0 0 0', '0')
or and several similar things but nothing is getting closer. a few things here are new to me and i’m reading the docs but can’t seem to piece together whats falling apart. Notable i don’t now why i get that last 0…
the input will eventually be many many lines but if it works for small case i’m sure it will translate over.
thanks
I wouldn’t use a regular expression for this. Instead use
grouperfrom the recipes on the itertools documentation:See it working online: ideone
Here’s the source code for
grouper(copied from the itertools documentation):