itertools.groupby

7stud bbxx789_05ss at yahoo.com
Sun May 27 13:17:52 EDT 2007


Bejeezus.  The description of groupby in the docs is a poster child
for why the docs need user comments.  Can someone explain to me in
what sense the name 'uniquekeys' is used this example:


import itertools

mylist = ['a', 1, 'b', 2, 3, 'c']

def isString(x):
    s = str(x)
    if s == x:
        return True
    else:
        return False

uniquekeys = []
groups = []
for k, g in itertools.groupby(mylist, isString):
    uniquekeys.append(k)
    groups.append(list(g))

print uniquekeys
print groups

--output:--
[True, False, True, False, True]
[['a'], [1], ['b'], [2, 3], ['c']]




More information about the Python-list mailing list