[Python-Dev] "groupby" iterator
Hye-Shik Chang
perky at i18n.org
Mon Dec 1 16:38:18 EST 2003
On Fri, Nov 28, 2003 at 01:46:53PM -0800, Guido van Rossum wrote:
> I would make one change: after looking at another use case, I'd like
> to change the outer iterator to produce (key, grouper) tuples. This
> way, you can write things like
>
> totals = {}
> for key, group in sequence:
> totals[key] = sum(group)
>
I think this would be helpful for lazy coders if some function of
itertools cover the use case: (`LIMIT' keyword of SQL)
>>> from groupby import groupby
>>> alwaystrue = lambda n: True
>>> for k, g in groupby(alwaystrue, range(20), 5):
... print list(g)
...
[0, 1, 2, 3, 4]
[5, 6, 7, 8, 9]
[10, 11, 12, 13, 14]
[15, 16, 17, 18, 19]
prototype of this case is groupby(keyfunc, iterable, limit=None).
Either, groupby(5, range(20)) is okay but 5 is not a sort of `key'. :)
Hye-Shik
More information about the Python-Dev
mailing list