On Fri, 25 Mar 2005 08:46:12 -0800, Michael Spencer <mahs at telcopartners.com> wrote:
>Tim Hochberg wrote:>> Jordan Rastrick wrote:>> >>itertools.groupby enables you to do this, you just need to define a suitable >grouping function, that stores its state:>>For example, if short lines should be appended to the previous line:>>from itertools import groupby>linesource = """\>Here is a long line, long line, long line>and this is short>and this is short>Here is a long line, long line, long line>and this is short""".splitlines()>>def record(item, seq = [0]):> if len(item) > 20:> seq[0] +=1> return seq[0]>>> >>> for groupnum, lines in groupby(linesource, record):> ... print "".join(lines)> ...> Here is a long line, long line, long lineand this is shortand this is short> Here is a long line, long line, long lineand this is short> >>>
Nice, but I think "record" is a bit opaque semantically.
How about group_id or generate_incrementing_unique_id_for_each_group_to_group_by or such?
Regards,
Bengt Richter