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: 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 Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 --f46d042186b9e095fa04dacde683 Content-Type: text/plain; charset=UTF-8 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. --f46d042186b9e095fa04dacde683 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
I have a file such as:

$ cat my_da= ta=C2=A0
Starting a new group
a
b
c
Starting a new group
1
2
3
4
Starting a new group
X
Y
Z
<= div>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
<= br>
def get_lines_from_file(file_name):
=C2=A0 =C2=A0 w= ith open(file_name) as reader:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 for li= ne in reader.readlines():
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 yield(line.strip())

counter =3D 0
def key_func(x):
=C2=A0 = =C2=A0 if x.startswith("Starting a new group"):
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 global counter
=C2=A0 =C2=A0 =C2=A0 =C2=A0 c= ounter +=3D 1
=C2=A0 =C2=A0 return counter

for key, group i= n groupby(get_lines_from_file("my_data"), key_func):
= =C2=A0 =C2=A0 print(list(group)[1:])
----------------------------= --------

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

--f46d042186b9e095fa04dacde683--