"groupby" is brilliant!

Alex Martelli aleax at mac.com
Wed Jun 14 10:59:05 EDT 2006


James Stroud <jstroud at ucla.edu> wrote:
   ...
> def doit(rows, doers, i=0):
>    for r, alist in groupby(rows, itemgetter(i)):
>      if len(doers) > 1:
>        doit(alist, doers[1:], i+1)
>      doers[0](r)

Isn't this making N useless slices (thus copies, for most kinds of
sequences) for a doers of length N?  Since you're passing i anyway, it
seems to me that:

def doit(rows, doers, i=0):
    for r, alist in groupby(rows, itemgetter(i)):
      if len(doers) > i+1:
         doit(alist, doers, i+1)
      doers[i](r)

is equivalent to your code, but avoids these slices (thus copies).


Alex



More information about the Python-list mailing list