推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
admirez
V2EX  ›  Python

有没有 reset 的 cumsum 函数

  •  
  •   admirez · Oct 30, 2020 via iPhone · 2087 views
    This topic created in 2202 days ago, the information mentioned may be changed or developed.
    比如[-1,-1,-1,1,1,1,1,-1,-1 ]。

    能变成 -1,-2,-3,1,2,3,4,-1,-2


    最好是高效率的
    necomancer
    necomancer
    Oct 30, 2020
    1
    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:])])
    necomancer
    necomancer
    Oct 30, 2020
    2
    可能快一点,尤其是 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]
    admirez
    admirez
    OP
    Oct 31, 2020 via iPhone
    3
    @necomancer 非常感谢,我试试看
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1412 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 16:51 · PVG 00:51 · LAX 08:51 · JFK 11:51
    ♥ Do have faith in what you're doing.