2.4 defines groupby, so there's no need to define our own.

This commit is contained in:
Jeremy Fincher 2005-02-04 05:55:04 +00:00
parent 566b273a97
commit 6580fc535f

View File

@ -27,6 +27,7 @@
# POSSIBILITY OF SUCH DAMAGE. # POSSIBILITY OF SUCH DAMAGE.
### ###
import sys
import new import new
import random import random
@ -48,21 +49,22 @@ def trueCycle(iterable):
if not yielded: if not yielded:
raise StopIteration raise StopIteration
def groupby(key, iterable): if sys.version_info < (2, 4, 0):
if key is None: def groupby(key, iterable):
key = lambda x: x if key is None:
it = iter(iterable) key = lambda x: x
value = it.next() # If there are no items, this takes an early exit it = iter(iterable)
oldkey = key(value) value = it.next() # If there are no items, this takes an early exit
group = [value] oldkey = key(value)
for value in it: group = [value]
newkey = key(value) for value in it:
if newkey != oldkey: newkey = key(value)
yield group if newkey != oldkey:
group = [] yield group
oldkey = newkey group = []
group.append(value) oldkey = newkey
yield group group.append(value)
yield group
def partition(p, iterable): def partition(p, iterable):
"""Partitions an iterable based on a predicate p. """Partitions an iterable based on a predicate p.