Re: Break up list into groups

2007-07-20 Thread Ron Adam
Matimus wrote: > 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: >

Re: Break up list into groups

2007-07-19 Thread Matimus
> 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: >

Re: Break up list into groups

2007-07-18 Thread George Sakkis
On Jul 17, 1:48 pm, Matimus <[EMAIL PROTECTED]> wrote: > I did some more experimenting and came up with the code below. It > shows several methods. When run, the script tests the robustness of > each method (roughly), and profiles it using timeit. The results from > running on my laptop are shown b

Re: Break up list into groups

2007-07-18 Thread Miki
Hello Dan, Yet another option (using itertools.groupby): from itertools import groupby class GrouperToggler: def __init__(self): self.group = 1 def __call__(self, value): # New packet, toggle group if value & 0x80: self.group = 1 - self.group

Re: Break up list into groups

2007-07-17 Thread Ron Adam
Matt McCredie wrote: > That certainly is fast, unfortunately it doesn't pass all of the tests. > I came up with those tests so I don't know how important they are to the > original poster. I modified it and came up with a generator and a > non-generator version based (roughly) on your algorith

Re: Break up list into groups

2007-07-17 Thread Matt McCredie
That certainly is fast, unfortunately it doesn't pass all of the tests. I came up with those tests so I don't know how important they are to the original poster. I modified it and came up with a generator and a non-generator version based (roughly) on your algorithm, that are almost as quick, and

Re: Break up list into groups

2007-07-17 Thread Ron Adam
Matimus wrote: > I did some more experimenting and came up with the code below. It > shows several methods. When run, the script tests the robustness of > each method (roughly), and profiles it using timeit. The results from > running on my laptop are shown below the code. Try this one... def g

Re: Break up list into groups

2007-07-17 Thread Matimus
I did some more experimenting and came up with the code below. It shows several methods. When run, the script tests the robustness of each method (roughly), and profiles it using timeit. The results from running on my laptop are shown below the code. seqs = [# Original: [0xF0, 1, 2, 3, 0

Re: Break up list into groups

2007-07-16 Thread James Stroud
Paul Rubin wrote: > See: > > http://groups.google.com/group/comp.lang.python/msg/2410c95c7f3b3654 Groupby is damn slow as far as I can tell (the Bates numbering in the above link assumes more than the OP intended, I assume). It looks like the author's original algorithm is the fastest python wa

Re: Break up list into groups

2007-07-16 Thread [EMAIL PROTECTED]
> Here's an ugly way I wouldn't recommended (using itertools groupby): > > py> from itertools import groupby > py> alist = [0xF0, 1, 2, 3, 0xF0, 4, 5, 6, > ... 0xF1, 7, 8, 0xF2, 9, 10, 11, 12, 13, > ... 0xF0, 14, 0xF1, 15] > py> def doit(alist): > ... i = (list(g) for k,g in gro

Re: Break up list into groups

2007-07-16 Thread [EMAIL PROTECTED]
On Jul 16, 3:56 pm, marduk <[EMAIL PROTECTED]> 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 t

Re: Break up list into groups

2007-07-16 Thread Paul Rubin
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes: > I can't seem to find an answer to this question anywhere, but I'm > still looking. My problem is I have a list of values like this: > > l = [0xF0, 1, 2, 3, 0xF0, 4, 5, 6, 0xF1, 7, 8, 0xF2, 9, 10, 11, 12, > 13, 0xF0, 14, 0xF1, 15] > > A value with

Re: Break up list into groups

2007-07-16 Thread Matimus
This is a perfect place for a generator: 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)

Re: Break up list into groups

2007-07-16 Thread James Stroud
James Stroud wrote: > Here's how I *would* do it: > > py> def doit(alist): > ... ary = [] > ... for i in alist: > ... if 0xf0 & i: > ... ary.append([i]) > ... else: > ... ary[-1].append(i) > ... return [x for x in ary if x] > ... To be absolutely compliant with the spec

Re: Break up list into groups

2007-07-16 Thread James Stroud
[EMAIL PROTECTED] wrote: > All, > > I can't seem to find an answer to this question anywhere, but I'm > still looking. My problem is I have a list of values like this: > > l = [0xF0, 1, 2, 3, 0xF0, 4, 5, 6, 0xF1, 7, 8, 0xF2, 9, 10, 11, 12, > 13, 0xF0, 14, 0xF1, 15] > > A value with bit 0x80 set

Re: Break up list into groups

2007-07-16 Thread marduk
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: > > groups = [] > current = [] # probably not necessary, but as a safety > for i in l: > if i >= 240: > current = [] > gr

Re: Break up list into groups

2007-07-16 Thread marduk
On Mon, 2007-07-16 at 14:11 -0700, [EMAIL PROTECTED] wrote: > I can't seem to find an answer to this question anywhere, but I'm > still looking. My problem is I have a list of values like this: > > l = [0xF0, 1, 2, 3, 0xF0, 4, 5, 6, 0xF1, 7, 8, 0xF2, 9, 10, 11, 12, > 13, 0xF0, 14, 0xF1, 15] > > A

Break up list into groups

2007-07-16 Thread [EMAIL PROTECTED]
All, I can't seem to find an answer to this question anywhere, but I'm still looking. My problem is I have a list of values like this: l = [0xF0, 1, 2, 3, 0xF0, 4, 5, 6, 0xF1, 7, 8, 0xF2, 9, 10, 11, 12, 13, 0xF0, 14, 0xF1, 15] A value with bit 0x80 set delineates the start of a new packet of inf