2

我有一个关于优化我的代码的问题。

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

我有一个包含周期性位码的熊猫系列。我的目标是删除在某个序列之前开始的所有条目,例如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:]

我感谢您的帮助。

4

2 回答 2

3

我们可以使用 来查看序列的滚动窗口视图view_as_windows,检查序列是否相等,并找到第一次出现的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
于 2020-05-18T10:50:53.810 回答
0

我会选择正则表达式 - 使用Web 界面来识别模式。

例如:

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

将产生一个 group(1) 输出,它由 1,1,0,0 模式之后的所有数字组成。

于 2020-05-18T11:07:40.363 回答