Newbie looking for elegant solution

kai.peters at gmail.com kai.peters at gmail.com
Wed Mar 25 00:19:06 EDT 2015


On Tuesday, 24 March 2015 21:04:37 UTC-7, Paul Rubin  wrote:
> nobody 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]
> ================================================================

no homework - real life. thanks for your contribution 



More information about the Python-list mailing list