On Wed, Mar 25, 2015 at 3:04 PM, Paul Rubin <no.email@nospam.invalid> 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 -- https://mail.python.org/mailman/listinfo/python-list