V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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
hitluobin
V2EX  ›  Python

python 大神,下面代码看不懂,求大神指导

  •  
  •   hitluobin · 2015-02-02 21:03:47 +08:00 · 3517 次点击
    这是一个创建于 3373 天前的主题,其中的信息可能已经有所发展或是发生改变。

    import itertools

    原tweet = "I luv my <3 iphone & youre awsm apple. DisplayIsAwesome, sooo happppppy :) http://www.apple.com"
    经过tweet = ''.join(''.join(s)[:2] for _, s in itertools.groupby(tweet))
    结果tweet = "I luv my <3 iphone & youre awsm apple. DisplayIsAwesome, soo happy :) http://ww.apple.com"

    求指教实现过程是怎么样的?

    5 条回复    2015-02-03 10:29:14 +08:00
    Shared
        1
    Shared  
       2015-02-02 21:13:05 +08:00   ❤️ 1
    查 itertools 的 groupby 用法
    letv
        2
    letv  
       2015-02-02 21:15:39 +08:00
    中文变量名。。。
    snachx
        3
    snachx  
       2015-02-03 08:48:24 +08:00
    @letv 中文只是lz的叙述过程。。。
    zerh925
        4
    zerh925  
       2015-02-03 09:06:53 +08:00
    学好语文再来提问
    xwsoul
        5
    xwsoul  
       2015-02-03 10:29:14 +08:00   ❤️ 1
    @zerh925 提问者不是问的很清楚了么...
    @hitluobin 按字符分组(itertools.groupby, 空格也算), 每组字符最多取两个 [:2] 合并为字符串, 将各组字符串合并为一个字符串
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1328 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 17:29 · PVG 01:29 · LAX 10:29 · JFK 13:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.