Newbie looking for elegant solution

Paul Rubin no.email at nospam.invalid
Wed Mar 25 00:04:10 EDT 2015


otaksoftspamtrap at gmail.com writes:
> I have a list containing 9600 integer elements - each integer is
> either 0 or 1.

Is that a homework problem?  This works for me in Python 2.7 but I think
Python 3 gratuitously broke tuple unpacking so it won't work there:

================================================================

from itertools import count, groupby
old = [0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1]
new = [reduce(lambda x,(y,i):x*2+y, g, 0)
       for k,g in groupby(zip(old,count()), lambda (a,b): b//8)]
print new

>>> [18, 222, 53]
================================================================



More information about the Python-list mailing list