将列表分成组 [英] Break up list into groups
问题描述
全部,
我似乎无法在任何地方找到这个问题的答案,但我仍然在寻找
。我的问题是我有一个这样的值列表:
l = [0xF0,1,2,3,0xF0,4,5,6,0xF1,7,8,0xF2, 9,10,11,12,
13,0xF0,14,0xF1,15]
设置位0x80的值描述了新的开始
信息包。我想要做的是对数据包进行分组,使得第一个数据包标记为0xF0,4,5,6与第二个数据包一起使用1,2,3
标记为0xF0,7& 8使用标记为0xF1的数据包,依此类推。与每个标签关联的数据的长度可以变化。我已经用b
编写了一个算法来做这个但我想知道是否有一些
组合的itertools函数可以更快地完成工作吗?
>
这就是我所做的和算法的预期输出:
def splitIntoGroups(数据):
groups = []
local = []
数据值:
if 0x80&价值:
如果len(本地)0:
groups.append(本地)
local = []
local.append(value)
else:
local.append(value)
if len(本地)0:
groups.append(本地)
返回群组
l = [0xF0,1, 2,3,0xF0,4,5,6,0xF1,7,8,12FF2,9,10,11,12,
13,0xF0,14,0xF1,15]
print splitIntoGroups(l)
所需结果:
[[240,1,2,3] ,[240,4,5,6],[241,7,8],[242,9,10,11,12,
13],[240,14],[241, 15]]
谢谢,
Dan McLeran
在星期一,2007-07-16 14:11 -0700, da ****** **@yahoo.com 写道:
我似乎无法在任何地方找到这个问题的答案,但我是
还在寻找。我的问题是我有一个这样的值列表:
l = [0xF0,1,2,3,0xF0,4,5,6,0xF1,7,8,0xF2, 9,10,11,12,
13,0xF0,14,0xF1,15]
设置位0x80的值描述了新的开始
信息包。我想要做的是对数据包进行分组,使得第一个数据包标记为0xF0,4,5,6与第二个数据包一起使用1,2,3
标记为0xF0,7& 8使用标记为0xF1的数据包,依此类推。与每个标签关联的数据的长度可以变化。我已经用b
编写了一个算法来做这个但我想知道是否有一些
组合的itertools函数可以更快地完成工作吗?
>
这就是我所做的和算法的预期输出:
def splitIntoGroups(数据):
groups = []
local = []
数据值:
if 0x80&价值:
如果len(本地)0:
groups.append(本地)
local = []
local.append(value)
else:
local.append(value)
if len(本地)0:
groups.append(本地)
返回群组
l = [0xF0,1, 2,3,0xF0,4,5,6,0xF1,7,8,12FF2,9,10,11,12,
13,0xF0,14,0xF1,15]
print splitIntoGroups(l)
所需结果:
[[240,1,2,3] ,[240,4,5,6],[241,7,8],[242,9,10,11,12,
13],[240,14],[241, 15]]
假设你的意思是''0xF0'而不是''0x80''....你的意思是任何价值
> = 240开始一个新组?如果是这样的话:
groups = []
current = []#可能没必要,但作为安全
$ b我在l的$ b:
如果i> = 240:
current = []
groups.append(current)
current.append(i)
On Mon,2007-07-16 at 16:31 -0500,marduk写道:
< blockquote class =post_quotes>
假设你的意思是''0xF0''而不是''0x80''....你的意思是任何价值
= 240开始一个新组?如果是这样的话:
groups = []
current = []#可能没必要,但作为安全
$ b我在l的$ b:
如果i> = 240:
current = []
groups.append(current)
current.append(i)
误解......实际上应该已经阅读了
groups = []
current = []#可能没必要,但作为一个安全
for i in l:
if 240&我:
current = []
groups.append(当前)
current.append(i)
这是一个发电机的理想场所:
< code>
seq = [0xF0,1 ,2,3,0FF0,4,5,6,0xF1,7,8,12FF,9,10,11,12,
13,0xF0,14,0xF1,15]
def gengroups(seq):
group = []
for val in seq:
if val &安培; 0x80和组:
收益组
group = []
group.append(val)
收益组
if __name__ ==" __ main __":
打印列表(gengroups(seq))
< / code>
以上假设输入序列中的第一个值将设置为
0x80。你的实现似乎也做了同样的假设
虽然。
另外,只是一个注释...
< code>
如果len(本地)0:
...
< / code>
写的更好
< code>
如果当地:
...
< / code>
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
information. What I want to do is to group the packets so that 1, 2, 3
go with the 1st packet tagged 0xF0, 4 ,5, 6 go with the 2nd packet
tagged 0xF0, 7 & 8 go with the packet tagged 0xF1 and so on. The
length of the data associated with each tag can vary. I''ve already
written an algorithm to do this but I was wondering if some
combination of itertools functions could do the job faster?
Here''s what I''ve done and the expected output of the algorithm:
def splitIntoGroups(data):
groups = []
local = []
for value in data:
if 0x80 & value:
if len(local) 0:
groups.append(local)
local = []
local.append(value)
else:
local.append(value)
if len(local) 0:
groups.append(local)
return groups
l = [0xF0, 1, 2, 3, 0xF0, 4, 5, 6, 0xF1, 7, 8, 0xF2, 9, 10, 11, 12,
13, 0xF0, 14, 0xF1, 15]
print splitIntoGroups(l)
Desired result:
[[240, 1, 2, 3], [240, 4, 5, 6], [241, 7, 8], [242, 9, 10, 11, 12,
13], [240, 14], [241, 15]]
Thanks,
Dan McLeran
On Mon, 2007-07-16 at 14:11 -0700, da********@yahoo.com 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 value with bit 0x80 set delineates the start of a new packet of
information. What I want to do is to group the packets so that 1, 2, 3
go with the 1st packet tagged 0xF0, 4 ,5, 6 go with the 2nd packet
tagged 0xF0, 7 & 8 go with the packet tagged 0xF1 and so on. The
length of the data associated with each tag can vary. I''ve already
written an algorithm to do this but I was wondering if some
combination of itertools functions could do the job faster?
Here''s what I''ve done and the expected output of the algorithm:
def splitIntoGroups(data):
groups = []
local = []
for value in data:
if 0x80 & value:
if len(local) 0:
groups.append(local)
local = []
local.append(value)
else:
local.append(value)
if len(local) 0:
groups.append(local)
return groups
l = [0xF0, 1, 2, 3, 0xF0, 4, 5, 6, 0xF1, 7, 8, 0xF2, 9, 10, 11, 12,
13, 0xF0, 14, 0xF1, 15]
print splitIntoGroups(l)
Desired result:
[[240, 1, 2, 3], [240, 4, 5, 6], [241, 7, 8], [242, 9, 10, 11, 12,
13], [240, 14], [241, 15]]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 = []
groups.append(current)
current.append(i)
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 = []
groups.append(current)
current.append(i)
Misunderstood... actually that should have read
groups = []
current = [] # probably not necessary, but as a safety
for i in l:
if 240 & i:
current = []
groups.append(current)
current.append(i)
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>
这篇关于将列表分成组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!