Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'none:': 0.07; 'lines.': 0.09; 'lines:': 0.09; 'properly.': 0.09; 'cc:addr :python-list': 0.11; 'def': 0.12; "wouldn't": 0.14; 'simplest': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'true:': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'raise': 0.29; 'message-id:@mail.gmail.com': 0.30; 'lines': 0.31; "d'aprano": 0.31; 'grouping': 0.31; 'steven': 0.31; 'subject:skip:i 10': 0.31; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'whilst': 0.36; 'yield': 0.36; 'skip:& 10': 0.38; 'break': 0.61; 'new': 0.61; 'simple': 0.61; 'yours': 0.88; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=gw0h3++EPC8AtanT+hAqyei8wcm+Kh1IKMCg972OmgQ=; b=E/c3zKMEYQV8O6zKl9BH7+YMTZdzBNixsv9kW+pq9olh7G6Ct60do0O6xn30eO+9RZ u3dkzmVFpUB9vQpFbhKsiQBjuNO13xgThnjubwrfWqgblOn2pXbkwTUMaNft2M6Msddt CCBR+zq20p2Q/D+5NeKgvLO2KH/Q/Id21gKKUAKMPGR5gu7yqnG6wXBnuivbp5fZJQpY qSCYRW9V+Re3UBHvhvYv7pTtse7bA+kSVU1yQeZcT0gGEgjUJndJaAnGj5l1rNumuR+6 wHdnFY0q2haWh6vDRdV2LzdMPIDhfzNoO+lAVXa46S1WveiUCdCGRgcNOE4tHqaOHseI 2bhQ== X-Received: by 10.112.173.225 with SMTP id bn1mr12427463lbc.92.1366600184350; Sun, 21 Apr 2013 20:09:44 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <51732f27$0$29977$c3e8da3$5496439d@news.astraweb.com> References: <51732f27$0$29977$c3e8da3$5496439d@news.astraweb.com> From: Joshua Landau Date: Mon, 22 Apr 2013 04:09:04 +0100 Subject: Re: itertools.groupby To: "Steven D'Aprano" Content-Type: multipart/alternative; boundary=001a11c33e4e89cc3304daea66a1 Cc: python-list X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 149 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366600191 news.xs4all.nl 2288 [2001:888:2000:d::a6]:58286 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44030 --001a11c33e4e89cc3304daea66a1 Content-Type: text/plain; charset=ISO-8859-1 On 21 April 2013 01:13, Steven D'Aprano < steve+comp.lang.python@pearwood.info> wrote: > I wouldn't use groupby. It's a hammer, not every grouping job is a nail. > > Instead, use a simple accumulator: > > > def group(lines): > accum = [] > for line in lines: > line = line.strip() > if line == 'Starting a new group': > if accum: # Don't bother if there are no accumulated lines. > yield accum > accum = [] > else: > accum.append(line) > # Don't forget the last group of lines. > if accum: yield accum > Whilst yours is the simplest bar Dennis Lee Bieber's and nicer in that it yields, neither of yours work for empty groups properly. I recommend the simple change: def group(lines): accum = None for line in lines: line = line.strip() if line == 'Starting a new group': if accum is not None: # Don't bother if there are no accumulated lines. yield accum accum = [] else: accum.append(line) # Don't forget the last group of lines. yield accum But will recommend my own small twist (because I think it is clever): def group(lines): lines = (line.strip() for line in lines) if next(lines) != "Starting a new group": raise ValueError("First line must be 'Starting a new group'") while True: acum = [] for line in lines: if line == "Starting a new group": break acum.append(line) else: yield acum break yield acum --001a11c33e4e89cc3304daea66a1 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On 21 April 2013 01:13, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
I wouldn't use groupby. It's a hammer, not= every grouping job is a nail.

Instead, use a simple accumulator:


def group(lines):
=A0 =A0 accum =3D []
=A0 =A0 for line in lines:
=A0 =A0 =A0 =A0 line =3D line.strip()
=A0 =A0 =A0 =A0 if line =3D=3D 'Starting a new group':
=A0 =A0 =A0 =A0 =A0 =A0 if accum: =A0# Don't bother if there are no acc= umulated lines.
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 yield accum
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 accum =3D []
=A0 =A0 =A0 =A0 else:
=A0 =A0 =A0 =A0 =A0 =A0 accum.append(line)
=A0 =A0 # Don't forget the last group of lines.
=A0 =A0 if accum: yield accum

Whi= lst yours is the simplest bar Dennis Lee Bieber's and nicer in that it = yields, neither of yours work for empty groups properly.
I recommend the simple change:

<= div style>
def group(lines):
=A0 =A0 accum =3D None
=A0 =A0 for line in lines:
=A0 =A0 =A0 =A0 line =3D line.strip()=
=A0 =A0 =A0 =A0 if line =3D=3D 'Starting a new group':
=A0 =A0 =A0 =A0 =A0 =A0 if accum is not None: =A0# Don't bother if= there are no accumulated lines.
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 = yield accum
=A0 =A0 =A0 =A0 =A0 =A0 accum =3D []
=A0 = =A0 =A0 =A0 else:
=A0 =A0 =A0 =A0 =A0 =A0 accum.append(line)
=A0 =A0 # Don't forget the last group of lines.
=A0 =A0 = yield accum

But will recommend my own small = twist (because I think it is clever):

def group(lines):
lines =3D (line.strip() for line in lines)

if next(lines) !=3D &q= uot;Starting a new group":
raise ValueError(&= quot;First line must be 'Starting a new group'")
while True:<= /div>
acum =3D []
<= div>
for = line in lines:
if line =3D=3D "Starting a new group":
break
=
acum.ap= pend(line)

else:
yield acum
<= div> break
yield acum=
--001a11c33e4e89cc3304daea66a1--