Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44030
| 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 | <joshua.landau.ws@gmail.com> |
| 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 | <mailman.855.1366477790.3114.python-list@python.org> <51732f27$0$29977$c3e8da3$5496439d@news.astraweb.com> |
| From | Joshua Landau <joshua.landau.ws@gmail.com> |
| Date | Mon, 22 Apr 2013 04:09:04 +0100 |
| Subject | Re: itertools.groupby |
| To | "Steven D'Aprano" <steve+comp.lang.python@pearwood.info> |
| Content-Type | multipart/alternative; boundary=001a11c33e4e89cc3304daea66a1 |
| Cc | python-list <python-list@python.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.895.1366600191.3114.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
itertools.groupby Jason Friedman <jsf80238@gmail.com> - 2013-04-20 11:09 -0600
Re: itertools.groupby Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-21 00:13 +0000
Re: itertools.groupby Joshua Landau <joshua.landau.ws@gmail.com> - 2013-04-22 04:09 +0100
Re: itertools.groupby Neil Cerutti <neilc@norwich.edu> - 2013-04-22 14:24 +0000
Re: itertools.groupby Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-04-22 15:49 +0100
Re: itertools.groupby Neil Cerutti <neilc@norwich.edu> - 2013-04-22 15:04 +0000
Re: itertools.groupby Chris Angelico <rosuav@gmail.com> - 2013-04-23 01:14 +1000
csiph-web