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
imcocc
V2EX  ›  Python

Python 多 for in 问题

  •  
  •   imcocc · 2017-01-14 23:44:31 +08:00 · 3557 次点击
    这是一个创建于 2630 天前的主题,其中的信息可能已经有所发展或是发生改变。

    list=["a","b","c","d"]

    for n in xrange(0,4,1):

    如何 打印出

    0,a

    1,b

    2,c

    3,d

    测试了几种都不对,不知道怎么写,请兄弟们指导一下。感谢

    16 条回复    2017-01-15 20:15:47 +08:00
    lhbc
        1
    lhbc  
       2017-01-14 23:47:06 +08:00   ❤️ 2
    print(n,list[n])
    zeroten
        2
    zeroten  
       2017-01-14 23:47:52 +08:00   ❤️ 1
    list=["a","b","c","d"]

    for k,v in enumerate(list):
    print(k,v)

    -------------
    (0, 'a')
    (1, 'b')
    (2, 'c')
    (3, 'd')


    楼主应该是想要这个吧
    cxyfreedom
        3
    cxyfreedom  
       2017-01-14 23:48:27 +08:00 via iPhone   ❤️ 1
    直接上 enumerate 啊
    Kilerd
        4
    Kilerd  
       2017-01-14 23:48:32 +08:00 via iPhone   ❤️ 1
    关键字 enumerate

    其他自行搜索
    imcocc
        5
    imcocc  
    OP
       2017-01-14 23:48:54 +08:00
    @lhbc 十分感谢 测试有效
    imcocc
        6
    imcocc  
    OP
       2017-01-14 23:50:21 +08:00
    @zeroten
    @cxyfreedom
    @Kilerd
    关于 enumerate 的用法 我也去搜索一下
    感谢分享
    imcocc
        7
    imcocc  
    OP
       2017-01-14 23:55:12 +08:00
    长见识了 enumerate 是个不错的内置函数,自动为数组或列表组成一个索引序列。
    用法
    for index,text in enumerate(list):

    print index ,text


    方便后来人
    Dvel
        8
    Dvel  
       2017-01-15 00:01:54 +08:00
    我也刚学了点 python 基础语法,好像没有 C 语言的 for-i 循环,只有 for-in ,配合 range 、 enumerate 之类的函数用起来也挺方便的。
    Allianzcortex
        9
    Allianzcortex  
       2017-01-15 00:14:29 +08:00
    这个......还是授人以渔更好些(汗。。其实自己就是弱鸡), Google 搜: Python iterate with index 第一个结果就是 SO 的这个 http://stackoverflow.com/questions/522563/accessing-the-index-in-python-for-loops 最高票回答就是用 enumerate 。换成 Scala iterate with index 就是 http://stackoverflow.com/questions/6833501/efficient-iteration-with-index-in-scala ,用 zipWithIndex 。用 Java iterate with index ,就是 http://stackoverflow.com/questions/3329842/how-to-get-the-current-loop-index-when-using-iterator ,本身没有实现,或者自己用一个变量来在 for-loop 里自增,或者用 iterator.nextIndex()
    Allianzcortex
        10
    Allianzcortex  
       2017-01-15 00:18:47 +08:00
    所以一直说掌握好 Google+英文 能解决开发中 99% 的问题~
    ericbize
        11
    ericbize  
       2017-01-15 00:20:21 +08:00 via iPhone
    @Allianzcortex 完全同意
    coolair
        12
    coolair  
       2017-01-15 02:37:29 +08:00 via Android
    我觉得楼主要的是 zip
    for a, b in zip(lista, listb)
    crab
        13
    crab  
       2017-01-15 03:51:41 +08:00
    list=["a","b","c","d"]
    for i in range(4):
    print(i,list[i],sep=',')
    ipwx
        14
    ipwx  
       2017-01-15 11:46:12 +08:00
    你可以看看 itertools 这个包,有很多好用的函数。
    forrestchang
        15
    forrestchang  
       2017-01-15 17:33:03 +08:00
    @ipwx enumerate 貌似是 built-in-functions 吧。

    这种情况肯定是 lz 对标准库不熟悉,建议系统地看一遍标准库。
    imcocc
        16
    imcocc  
    OP
       2017-01-15 20:15:47 +08:00 via iPhone
    @forrestchang 嗯 正在看标准库,有个印象用的时候知道怎么搜了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1280 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 17:50 · PVG 01:50 · LAX 10:50 · JFK 13:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.