Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '[0,': 0.09; 'broke': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '2.7': 0.14; 'count,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'itertools': 0.16; 'lambda': 0.16; 'tuple': 0.16; 'unpacking': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'paul': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'message- id:@mail.gmail.com': 0.30; '25,': 0.31; '>>>>': 0.31; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'skip:[ 10': 0.38; 'pm,': 0.38; 'skip:x 10': 0.40; 'new': 0.61; 'mar': 0.68; '2015': 0.84; 'subject:looking': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=RfUTUFTouC+QBTN6JPy+2wjYjvybHOKgpJY2s+lk7HA=; b=BMzUlc/+tE17g+GiwopJShjoBdoXci2hhIsSDwPhF1fK2LXMZpR2cAy1cKT7Q5uQab oxWS7TOiRC2XJyt1ttHPJknMEIkbLilG1Nze6o0WK31I7irynaN2aL+yXtweJ+7jrJm/ sN182By3xpLZ0NtDxYm28FqiT08T3lmB5esKsGTILzBehIXJIcqkj85b1hJQmcs/LTnM vSY0opXU8ZrJsVacGj26Lbu5MTiN3UPNGl7HqSE7ZeRuJbis143rPK1a8+d6em8cbHDu nzhZzKHtJVFui6zp4gbRo2NhhWDHfIvBT7Npu7JPiY52CTMv/Eni1bs5Pt5umhjDIZVP 5RLg== MIME-Version: 1.0 X-Received: by 10.43.38.144 with SMTP id ti16mr30053022icb.26.1427257195570; Tue, 24 Mar 2015 21:19:55 -0700 (PDT) In-Reply-To: <874mp97mf9.fsf@jester.gateway.sonic.net> References: <874mp97mf9.fsf@jester.gateway.sonic.net> Date: Wed, 25 Mar 2015 15:19:55 +1100 Subject: Re: Newbie looking for elegant solution From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1427257197 news.xs4all.nl 2968 [2001:888:2000:d::a6]:45392 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87927 On Wed, Mar 25, 2015 at 3:04 PM, Paul Rubin 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