itertools.groupby
Paul Rubin
http
Tue May 29 23:30:22 EDT 2007
Raymond Hettinger <python at rcn.com> writes:
> The gauntlet has been thrown down. Any creative thinkers
> up to the challenge? Give me cool recipes.
Here is my version (with different semantics) of the grouper recipe in
the existing recipe section:
snd = operator.itemgetter(1) # I use this so often...
def grouper(seq, n):
for k,g in groupby(enumerate(seq), lambda (i,x): i//n):
yield imap(snd, g)
I sometimes use the above for chopping large (multi-gigabyte) data
sets into manageable sized runs of a program. That is, my value of n
might be something like 1 million, so making tuples that size (as the
version in the itertools docs does) starts being unpleasant. Plus,
I think the groupby version makes more intuitive sense, though it
has pitfalls if you do anything with the output other than iterate
through each item as it emerges. I guess you could always use map
instead of imap.
More information about the Python-list
mailing list