itertools.groupby

Jason Friedman jsf80238 at gmail.com
Sun Apr 21 12:44:44 EDT 2013


#!/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:])
>
>
Thank you for the responses!  Not sure yet which one I will pick.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130421/0f64afd3/attachment.html>


More information about the Python-list mailing list