Break up list into groups

danmcleran at yahoo.com danmcleran at yahoo.com
Tue Jul 17 00:52:13 CEST 2007


On Jul 16, 3:56 pm, marduk <mar... at nbk.hopto.org> wrote:
> On Mon, 2007-07-16 at 16:31 -0500, marduk wrote:
> > Assuming you meant '0xF0' instead of '0x80'.... do you mean any value
> > >=240 starts a new group?  If so:
No, I meant 0x80. 0x80 is only the most significant bit of the 0xF0
value. Every time this bit is set indicates the start of a new group.
Anyway, I like this algorithm better than mine, less code. I've
modified yours to make the fn a parameter:

def splitIntoGroups(data, fn):
    groups  = []
    current = []
    for i in data:
        if fn(i):
            current = []
            groups.append(current)
        current.append(i)

    return groups

print splitIntoGroups(l, lambda x : x & 0x80)




More information about the Python-list mailing list