查找序列并删除以前的条目

Find a sequence and remove previous entries

我对我的代码优化有疑问。

Signal = pd.Series([1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0])

我有一个包含周期性位码的 pandas 系列。我的目标是删除在某个序列之前开始的所有条目,例如1,1,0,0。所以在我的例子中,我希望有一个像这样的简化系列:

[1, 1, 0, 0, 1, 1, 0, 0]

我已经有了 1, 1 的解决方案,但它不是很优雅,而且对于我的示例来说也不容易修改:1,1,0,0

    i = 0
    bool = True
    while bool:
        a = Signal.iloc[i]
        b = Signal.iloc[i + 1]
        if a == 1 and b == 1:
            bool = False
        else:
            i = i + 1

   Signal = Signal[i:]

感谢您的帮助。

我们可以滚动 window 观看系列 view_as_windows, check for equality with the sequence and find the first occurrence with argmax:

from skimage.util import view_as_windows

seq = [1, 1, 0, 0]
m = (view_as_windows(Signal.values, len(seq))==seq).all(1)
Signal[m.argmax():]

3     1
4     1
5     0
6     0
7     1
8     1
9     0
10    0
dtype: int64

我会选择正则表达式 - 使用 web interface 来识别模式。

例如:

1,1,0,0,(.*\d)

将产生 group(1) 输出,其中包含 1,1,0,0 模式之后的所有数字。