如果列表包含特定的初始元素
,我如何从列表中提取元素这是列表数据= [10, 16,2 ,45,52,12,0,0,2,0,54,85,23,15,48,78,45 ,16,16,0,4, 16,3 ,16,2,0,....]
示例数据: -
1) [10,16,2,45,52,12,0,0,2,0,78,45,16,16,0,4,16,3,16,2,0,....] (upto 200bytes)
2) [16,2,24,14,45,25,87,89,23,0,0,5,10,16,0,4,16,3,16,2,0,....]
3) [11,12,24,14,45,25, 16,2, 121,45,0,10,16,0,4,16,3,16,2,0,....]
首先我必须搜索 16,2 元素并提取 16,2 之后的所有元素直到它找到元素 16,3 并丢弃之后的每个重复帧
根据数据流,初始字节可以在列表的开头,也可以从流中的任何位置开始
答案 0 :(得分:4)
您可以使用zip
函数创建对并获取开始和结束对的索引,使用切片来提取预期元素:
>>> def extracter(lst,start,end):
... pairs = zip(lst,lst[1:])
... start_index = pairs.index(tuple(start))+2
... end_index = pairs.index(tuple(end))
... return lst[start_index:end_index]
...
演示:
>>> lst = [10, 16,2, 45,52,12,0,0,2,0,78,45,16,16,0,4, 16,3, 16,2,0]
>>> extracter(lst,(16,2),(16,3))
[45, 52, 12, 0, 0, 2, 0, 78, 45, 16, 16, 0, 4]
答案 1 :(得分:3)
这应该是相当有效的,虽然有点冗长
from itertools import islice
def n_grams(a, n):
z = (islice(a, i, None) for i in range(n))
return zip(*z)
def filterStartStop(lst, start, stop):
it = n_grams(lst, 3)
for i in it:
if i[0:2] == start:
break
for i in it:
if i[1:3] == stop:
break
yield i[1]
a = range(10)
print(list(filterStartStop(a, (1,2), (7,8))))
>>[3, 4, 5, 6]
与@Kasramvd解决方案相比。
a = range(10**5)
start = (100, 101)
stop = (12345,12346)
%timeit list(filterStartStop(a, start, stop))
%timeit list(extracter(a, start, stop))
b = list(filterStartStop(a, start, stop))
c = list(extracter(a, start, stop))
print (all(i == j for i,j in zip(b,c)))
>>100 loops, best of 3: 6.18 ms per loop
>>100 loops, best of 3: 19.4 ms per loop
>>True
答案 2 :(得分:1)
我认为我找到了解决方案。
def findIn(l):
foundBeginning=False
foundEnding=False
for i in range(len(l)):
if l[i] == 16:
if l[i+1] == 2:
if foundBeginning == False and foundEnding == False:
begin = i+2
foundBeginning = True
if l[i+1] == 3:
if foundBeginning == True and foundEnding == False:
end = I
foundEnding = True
return(l[begin:end])