推荐学习书目
› 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
going
V2EX  ›  Python

Python 列表的相邻正负数合并有啥好方法嘛?

  •  
  •   going · Mar 2, 2021 · 1926 views
    This topic created in 2041 days ago, the information mentioned may be changed or developed.

    请问 Python 列表的相邻正负数合并有啥好方法嘛?

    比如 meta = [0.24, -0.6, -0.68, 0.73, 0.34] 这个列表需要改为 meta = [0.24, -1.28, 1.07]

    3 replies  •  2021-03-02 17:02:49 +08:00
    013231
        1
    013231  
       Mar 2, 2021   ❤️ 7
    from itertools import groupby

    meta = [sum(g) for _, g in groupby(meta, key=lambda v: v > 0)]
    renmu123
        2
    renmu123  
       Mar 2, 2021
    双指针吧,O(N)复杂度
    rrfeng
        3
    rrfeng  
       Mar 2, 2021
    遍历一遍不就完事儿了?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1663 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 16:13 · PVG 00:13 · LAX 09:13 · JFK 12:13
    ♥ Do have faith in what you're doing.