我将信号存储为非负数列表。我看起来很像:
00000000012340000000111111111110000000
查找零段的所有位置的最快方法是什么。我需要每个细分的开始,结束和中间点。用任何值表示我的开始。
答案 0 :(得分:0)
可能这是一个答案:
signal = [0,0,0,0,1,2,3,4,0,0,0,0,1,1,1,1,1,0,0,0,0]
segments = []
segmentsCoords = []
inSegment = False
for i in xrange(len(signal)):
if inSegment and signal[i] == 0:
continue
elif not inSegment and signal[i] == 0:
inSegment = True
begin = i
elif inSegment and signal[i] != 0:
inSegment = False
end = i
segments.append((end-begin)/2+begin)
segmentsCoords.append([begin, end])
elif not inSegment and signal[i]:
continue
if signal[-1] == 0:
end = len(signal)
segments.append((end-begin)/2+begin)
segmentsCoords.append([begin, end])
print segmentsCoords
print segments