Break up list into groups

Matimus mccredie at gmail.com
Thu Jul 19 17:44:41 CEST 2007


> A little renaming of variables helps it be a bit more elegant I think ...
>
> def getgroups8(seq):
>      groups = []
>      iseq = iter(xrange(len(seq)))
>      for start in iseq:
>          if seq[start] & 0x80:
>              for stop in iseq:
>                  if seq[stop] & 0x80:
>                      groups.append(seq[start:stop])
>                      start = stop
>              groups.append(seq[start:])
>      return groups

Excellent work! One more modification and I get below 10us/pass:

def getgroups(seq):
     groups = []
     push = groups.append
     iseq = iter(xrange(len(seq)))
     for start in iseq:
         if seq[start] & 0x80:
             for stop in iseq:
                 if seq[stop] & 0x80:
                     push(seq[start:stop])
                     start = stop
             push(seq[start:])
     return groups

-Matt




More information about the Python-list mailing list