python 初学者又提问了 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
xcc7624

python 初学者又提问了

  •  
  •   xcc7624 Jul 17, 2014 3402 views
    This topic created in 4302 days ago, the information mentioned may be changed or developed.
    lst = range(10)

    for item in lst:
    lst.pop(0)
    print item
    print lst
    #结果
    0
    0
    1
    2
    2
    4
    3
    6
    4
    8
    [5, 6, 7, 8, 9]
    为什么会是这样
    O21
        1
    O21  
       Jul 17, 2014
    for i in range(10):
    print i

    这样写比较简单吧。。
    puyo
        2
    puyo  
       Jul 17, 2014
    list.pop([i])
    Remove the item at the given position in the list, and return it.



    懂了嘛。
    xcc7624
        3
    xcc7624  
    OP
       Jul 17, 2014 via Android
    @puyo为什么只迭代了五次?
    puyo
        4
    puyo  
       Jul 17, 2014   1
    @xcc7624 因为进入如循环后要去取lst中对应item的下标。
    第一次取第0个,得到的是0
    第二次取第1个,这时候就是从pop之后的列表去取了,是2
    ...
    第五次得到的就是8,这时候lst表中只有5个数字里。
    在第六次的时候,越界了。
    xcc7624
        5
    xcc7624  
    OP
       Jul 17, 2014 via Android
    @puyo越界了为什么没抛出异常
    kqz901002
        6
    kqz901002  
       Jul 17, 2014
    @puyo 这样写code有没有实际意义?或者这样写貌似会出现误解啊
    kqz901002
        7
    kqz901002  
       Jul 17, 2014
    @xcc7624 他说的越界的意思是 目前lst只有五个元素,五次循环结束了,就应该结束循环
    puyo
        8
    puyo  
       Jul 17, 2014
    @kqz901002 没什么意义吧,就算要间隔取数加step就好了。这样写也比较难以理解。
    vellow
        9
    vellow  
       Jul 17, 2014
    是不是这样,for循环的index+1不会每次都去查询lst具体有多长,只有遇到终点就结束,所以旧的index会应用到新的lst上?
    About     Help     Advertise     Blog     API     FAQ     Solana     4014 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 45ms UTC 05:08 PVG 13:08 LAX 22:08 JFK 01:08
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86