Added itertools.groupby.

This commit is contained in:
Jeremy Fincher 2004-02-21 10:29:10 +00:00
parent d0547ba954
commit 9f766c154a
1 changed files with 17 additions and 0 deletions

View File

@ -78,6 +78,23 @@ def ilen(iterator):
return i
itertools.ilen = ilen
def groupby(key, iterable):
if key is None:
key = lambda x: x
it = iter(iterable)
value = it.next() # If there are no items, this takes an early exit
oldkey = key(value)
group = [value]
for value in it:
newkey = key(value)
if newkey != oldkey:
yield group
group = []
oldkey = newkey
group.append(value)
yield group
itertools.groupby = groupby
def group(seq, groupSize, noneFill=True):
"""Groups a given sequence into sublists of length groupSize."""
ret = []