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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'output': 0.05; 'none:': 0.07; 'lines:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'wrote': 0.14; "'b',": 0.16; '1:09': 0.16; "['a',": 0.16; 'delicate': 0.16; 'itertools': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'wrote:': 0.18; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'cc:2**0': 0.24; 'handling': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'wondering': 0.29; 'skip:g 30': 0.30; "i'm": 0.30; 'assert': 0.31; 'subject:skip:i 10': 0.31; 'file': 0.32; 'skip:- 30': 0.32; 'skip:# 10': 0.33; 'but': 0.35; 'there': 0.35; 'yield': 0.36; 'should': 0.36; 'list': 0.37; 'starting': 0.37; 'jason': 0.38; 'pm,': 0.38; 'skip:p 20': 0.39; 'new': 0.61; 'first': 0.61; 'such': 0.63; 'to:addr:gmail.com': 0.65; 'gotten': 0.74; 'as:': 0.81; "'2',": 0.84; "'3',": 0.84; 'lists:': 0.91; 'wanting': 0.93 Date: Sat, 20 Apr 2013 13:27:18 -0400 From: Ned Batchelder User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 MIME-Version: 1.0 To: Jason Friedman Subject: Re: itertools.groupby References: In-Reply-To: Content-Type: multipart/alternative; boundary="------------000907030309040500000201" Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 192 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366478853 news.xs4all.nl 2189 [2001:888:2000:d::a6]:52644 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:43960 This is a multi-part message in MIME format. --------------000907030309040500000201 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 4/20/2013 1:09 PM, Jason Friedman wrote: > 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. > > > def separate_on(lines, separator): group = None for line in lines: if line.strip() == separator: if group is not None: yield group group = [] else: assert group is not None # Should have gotten a separator first group.append(line) yield group with open("my_data") as my_data: for group in separate_on(my_data, "Starting a new group"): print group The handling of the first separator line feels delicate to me, but this provides the output you want. --Ned. --------------000907030309040500000201 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On 4/20/2013 1:09 PM, Jason Friedman wrote:
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.




def separate_on(lines, separator):
    group = None
    for line in lines:
        if line.strip() == separator:
            if group is not None:
                yield group
            group = []
        else:
            assert group is not None # Should have gotten a separator first
            group.append(line)
    yield group

with open("my_data") as my_data:
    for group in separate_on(my_data, "Starting a new group"):
        print group


The handling of the first separator line feels delicate to me, but this provides the output you want.

--Ned.
--------------000907030309040500000201--