推荐学习书目
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 · 1740 views
    This topic created in 1925 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   ·   Solana   ·   5508 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 06:43 · PVG 14:43 · LAX 23:43 · JFK 02:43
    ♥ Do have faith in what you're doing.