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

小白遇到个字符串数据处理,请教一下

  •  
  •   hellogitooxx · Apr 3, 2021 · 3152 views
    This topic created in 2002 days ago, the information mentioned may be changed or developed.
    字符串如下(部分),

    LWWLWLWWLLLLLWLLLWLWLWWWLWLWWWLWLWLLWLLLLWLLLWLWLWWW

    从头开始,统计,遇到相同的数据,统计一次,如,L 统计成 1,后面遇到 WW 统计成 2 次,又遇到 L 统计成 1 次,W 统计成 1 次,以此类推。

    想得到的结果,
    L(1),W(2),L(1),W(1),L(1),W(2),L(5),........

    然后生成图表(折线图或柱状图)
    5 replies  •  2021-04-07 22:07:06 +08:00
    eason1874
        1
    eason1874  
       Apr 3, 2021   ❤️ 1
    作业?老题目了,我没写 Python 都看过这题。随手一搜就有答案。

    import itertools

    result = [(k, len(list(g))) for k, g in itertools.groupby('LWWLWLWWLLLLLWLLLWLWLWWWLWLWWWLWLWLLWLLLLWLLLWLWLWWW')]

    print(result)
    hellogitooxx
        2
    hellogitooxx  
    OP
       Apr 3, 2021
    @eason1874 感谢。
    imn1
        3
    imn1  
       Apr 3, 2021   ❤️ 1
    #1 这个是手册上的例程,看样子没看过 itertools 这章
    xihuannihesuanna
        4
    xihuannihesuanna  
       Apr 7, 2021
    @imn1 老哥 什么手册呐 🤣🤣 求指路
    imn1
        5
    imn1  
       Apr 7, 2021
    @xihuannihesuanna #4
    这问题怎么答啊?
    怎么还有人写 Python 不知道手册的?全靠猜么?

    https://docs.python.org
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2521 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 05:35 · PVG 13:35 · LAX 22:35 · JFK 01:35
    ♥ Do have faith in what you're doing.