Newbie looking for elegant solution

kai.peters at gmail.com kai.peters at gmail.com
Wed Mar 25 00:46:23 EDT 2015


On Tuesday, 24 March 2015 21:20:11 UTC-7, Chris Angelico  wrote:
> On Wed, Mar 25, 2015 at 3:04 PM, Paul Rubin <nobody> wrote:
> > 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]
> > ================================================================
> 
> You don't need tuple unpacking. Here's the Py3 version of the above:
> 
> from functools import reduce
> new = [reduce(lambda x,y:x*2+y[0], g, 0)
>     for k,g in groupby(zip(old,count()), lambda a: a[1]//8)]
> 
> ChrisA


Now I have just read the latest spec and speed/memory may become issues:

1 bit images of a size of 1024 x 1280 need to be processed this way, so
1310720 list elements. Also needs to be 2.7 only.

Any recommendations?



More information about the Python-list mailing list