推荐学习书目
› 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 · 1991 views
    This topic created in 2157 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
        1
    necomancer  
       Oct 30, 2020
    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
        2
    necomancer  
       Oct 30, 2020
    可能快一点,尤其是 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
        3
    admirez  
    OP
       Oct 31, 2020 via iPhone
    @necomancer 非常感谢,我试试看
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2588 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 11:46 · PVG 19:46 · LAX 04:46 · JFK 07:46
    ♥ Do have faith in what you're doing.