例如,我有一个 python 列表:
mylist = [1,1,1,1,1,1,1,1,1,1,1,
0,0,1,1,1,1,0,0,0,0,0,
1,1,1,1,1,1,1,1,0,0,0,0,0,0]
Run Code Online (Sandbox Code Playgroud)
我的目标是找到连续有五个或更多零的位置,然后列出发生这种情况的索引,例如,输出为:
[17,21][30,35]
Run Code Online (Sandbox Code Playgroud)
这是我在此处提出的其他问题中尝试/看到的内容:
def zero_runs(a):
# Create an array that is 1 where a is 0, and pad each end with an extra 0.
iszero = np.concatenate(([0], np.equal(a, 0).view(np.int8), [0]))
absdiff = np.abs(np.diff(iszero))
# Runs start and end where absdiff is 1.
ranges = np.where(absdiff == 1)[0].reshape(-1, 2)
return ranges
runs = zero_runs(list)
Run Code Online (Sandbox Code Playgroud)
这给出了输出:
[0,10]
[11,12]
...
Run Code Online (Sandbox Code Playgroud)
这基本上只是列出所有重复项的索引,我将如何将此数据分离为我需要的数据