Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #43960
| Date | 2013-04-20 13:27 -0400 |
|---|---|
| From | Ned Batchelder <ned@nedbatchelder.com> |
| Subject | Re: itertools.groupby |
| References | <CANy1k1jVD3xDQGVdGRuCycg4yXojtqKfAzZFM=C88SsHnqrLQQ@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.857.1366478853.3114.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
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.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: itertools.groupby Ned Batchelder <ned@nedbatchelder.com> - 2013-04-20 13:27 -0400
csiph-web