1
necomancer 2020-10-30 16:40:46 +08:00
a = np.array([-1,-1,-1,1,1,1,1,-1,-1])
bounds = np.r_[0, np.argwhere(np.diff(a>0, prepend=0)).ravel(), a.shape[0]] np.concatenate([np.cumsum(a[i:j]) for i, j in zip(bounds[:-1], bounds[1:])]) |
2
necomancer 2020-10-30 16:53:32 +08:00
可能快一点,尤其是 1, -1 交错很厉害的时候
import pandas as pd b = pd.Series(np.insert(a, np.argwhere(np.diff(a>0, prepend=0)).ravel(), 0)) cum = b.cumsum() cum = (cum -cum.where(b==0).ffill().fillna(0)).values cum = cum[cum!=0] |
3
admirez OP @necomancer 非常感谢,我试试看
|