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

这是什么用法?

  •  
  •   displayabc · Sep 17, 2014 · 3799 views
    This topic created in 4396 days ago, the information mentioned may be changed or developed.
    a = [1, 2, 3, 4]
    print(','.join('%s' % id for id in a))

    join里边的这段是什么意思?
    4 replies  •  2014-09-18 22:06:51 +08:00
    kzing
        1
    kzing  
       Sep 18, 2014   ❤️ 1
    join里面是一个生成器表达式, '%s'用来转字符串. 相当于以下用法.

    ','.join(str(id) for id in a)

    # or

    ','.join(map(str, a))
    hiddenman
        2
    hiddenman  
       Sep 18, 2014
    多看看Python基础书籍。
    Melodic
        3
    Melodic  
       Sep 18, 2014   ❤️ 1
    人家问join里面的 是什么意思 - -就是表推导嘛
    billlee
        4
    billlee  
       Sep 18, 2014   ❤️ 1
    '%s' % id 是把变量 id format 成字符串
    ('%s' % id for id in a) 是一个 generator, 就是字面的意义,迭代 a 中的每个元素 id, 返回 '%s' % id
    如果把圆括号改成放括号,即 ['%s' % id for id in a], 就是返回一个列表 ['1', '2', '3', '4']
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2442 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 08:54 · PVG 16:54 · LAX 01:54 · JFK 04:54
    ♥ Do have faith in what you're doing.