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

用列表解析的时候可以输出下标么

  •  
  •   jianghu52 · May 27, 2014 · 3645 views
    This topic created in 4369 days ago, the information mentioned may be changed or developed.
    各位就当我闲的蛋疼,想问一下有没有办法在列表解析的时候同时输出下标。

    lista = ['a', 'b', 'c']
    print [x for x in lista]

    此时输出的还是 ['a', 'b', 'c']
    我希望输出的是 [('a', 0), ('b', 1), ('c', 2)]
    请问有什么办法
    5 replies    2014-05-28 08:15:24 +08:00
    jiang42
        2
    jiang42  
       May 27, 2014
    cbsw
        3
    cbsw  
       May 27, 2014   ❤️ 1
    [(i,j) for i, j in zip(lista, range(len(lista)))]
    dbow
        4
    dbow  
       May 27, 2014
    这样: [(v, i) for i, v in enumerate(lista)]
    RIcter
        5
    RIcter  
       May 28, 2014
    @dbow 这是正解。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1127 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 18:04 · PVG 02:04 · LAX 11:04 · JFK 14:04
    ♥ Do have faith in what you're doing.