Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #43957

itertools.groupby

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <jsf80238@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.077
X-Spam-Evidence '*H*': 0.85; '*S*': 0.01; 'output': 0.05; 'cc:addr :python-list': 0.11; 'def': 0.12; 'wrote': 0.14; "'b',": 0.16; "['a',": 0.16; 'itertools': 0.16; 'skip:# 20': 0.16; 'skip:g 40': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'this:': 0.26; 'wondering': 0.29; 'skip:g 30': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'subject:skip:i 10': 0.31; 'file': 0.32; 'skip:- 30': 0.32; 'skip:# 10': 0.33; 'skip:t 40': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'list': 0.37; 'starting': 0.37; 'skip:p 20': 0.39; '8bit%:6': 0.40; 'new': 0.61; 'such': 0.63; 'as:': 0.81; "'2',": 0.84; "'3',": 0.84; 'lists:': 0.91; 'to:none': 0.92; 'wanting': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:date:message-id:subject:from:cc :content-type; bh=/X2zVOC4efA8I2XQaC7wHzHuVx5THNJv6+MvN/4Iu5Y=; b=LkL77uwe07j86izEoCWCfxZ+TE5Pt8EkQNJGdv4l218TdW2e+QdrrqD6UEj1nrliYV qzBUYGk8IIkJwLHT4xWRphe5Dd6AyCm4vXuv6GBhb/LJz93/mq3cTpS+/8PAumShJSeW PzG+30i+GNoLWF4tcG783AsICBVKbldPqmNbdfTiGNTc32YIezDaJfdXacM87a1MckeW KwrjoPefVacLuCLTTWNxv13UWFryN1jUVotH7gR3CCh7IxKYsxRR6QF9Wzp2Vt4t2omm 0hXh1fvBvzPGNWD8n4MGjHbGiRxhi1mVJXLVdxgb/yqTetssdprXj9lsqVLQUsOiGbfk cbxg==
MIME-Version 1.0
X-Received by 10.205.105.8 with SMTP id do8mr7731407bkc.3.1366477783454; Sat, 20 Apr 2013 10:09:43 -0700 (PDT)
Date Sat, 20 Apr 2013 11:09:42 -0600
Subject itertools.groupby
From Jason Friedman <jsf80238@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type multipart/alternative; boundary=f46d042186b9e095fa04dacde683
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.855.1366477790.3114.python-list@python.org> (permalink)
Lines 85
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1366477790 news.xs4all.nl 2315 [2001:888:2000:d::a6]:42737
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:43957

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

I have a file such as:

$ cat my_data
Starting a new group
a
b
c
Starting a new group
1
2
3
4
Starting a new group
X
Y
Z
Starting a new group

I am wanting a list of lists:
['a', 'b', 'c']
['1', '2', '3', '4']
['X', 'Y', 'Z']
[]

I wrote this:
------------------------------------
#!/usr/bin/python3
from itertools import groupby

def get_lines_from_file(file_name):
    with open(file_name) as reader:
        for line in reader.readlines():
            yield(line.strip())

counter = 0
def key_func(x):
    if x.startswith("Starting a new group"):
        global counter
        counter += 1
    return counter

for key, group in groupby(get_lines_from_file("my_data"), key_func):
    print(list(group)[1:])
------------------------------------

I get the output I desire, but I'm wondering if there is a solution without
the global counter.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


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