Break up list into groups

Matimus mccredie at gmail.com
Mon Jul 16 18:18:44 EDT 2007


This is a perfect place for a generator:

<code>
seq = [0xF0, 1, 2, 3, 0xF0, 4, 5, 6, 0xF1, 7, 8, 0xF2, 9, 10, 11, 12,
13, 0xF0, 14, 0xF1, 15]

def gengroups(seq):
    group = []
    for val in seq:
        if val & 0x80 and group:
            yield group
            group = []
        group.append(val)
    yield group

if __name__ == "__main__":
    print list(gengroups(seq))
</code>

The above assumes that the first value in the input sequence will have
0x80 set. Your implementation seems to makes the same assumption
though.

Also, just a note...
<code>
if len(local) > 0:
 ...
</code>

is better written

<code>
if local:
 ...
</code>




More information about the Python-list mailing list