comple list slices

johnzenger at gmail.com johnzenger at gmail.com
Tue Feb 28 12:10:26 EST 2006


Although I don't know if this is faster or more efficient than your
current solution, it does look cooler:

def grouprows(inrows):
    rows = []
    rows[:] = inrows  #  makes a copy because we're going to be
deleting
    while len(rows) > 0:
        rowspan = rows[0]["rowspan"]
        yield rows[0:rowspan]  # "returns" this value, but control flow
unaffected
        del rows[0:rowspan]  # remove what we just returned from the
list, and loop

grouper = grouprows(copyrows)
print [x for x in grouper]

This is basically just a simple function that rips chunks off the front
of a list and returns them.  Because the function uses "yield" rather
than "return," it becomes a generator, which can be treated by Python
as an iterator.




More information about the Python-list mailing list