菜鸡求教,为什么从切片删除数目不对 - V2EX
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
wiZ90
V2EX    Python

菜鸡求教,为什么从切片删除数目不对

  •  
  •   wiZ90 2017-09-07 14:56:53 +08:00 3043 次点击
    这是一个创建于 2957 天前的主题,其中的信息可能已经有所发展或是发生改变。
    书上习题写完了没事儿写着玩,问题在第 13 行开始,我想把 C = red 的都删除掉
    为什么我的运行结果只剩下了一个?

    alines = []
    for alines_No in range (5):
    new_aline = {"C":"red","Point":"10","Speed":"5"}
    alines.append(new_aline)

    for aline in alines:
    print (aline)
    print (len(alines))

    for aline in alines[:2]:
    if aline['C'] == 'red':
    del alines[:2]
    print (alines)


    run

    {'C': 'red', 'Point': '10', 'Speed': '5'}
    {'C': 'red', 'Point': '10', 'Speed': '5'}
    {'C': 'red', 'Point': '10', 'Speed': '5'}
    {'C': 'red', 'Point': '10', 'Speed': '5'}
    {'C': 'red', 'Point': '10', 'Speed': '5'}
    5
    [{'C': 'red', 'Point': '10', 'Speed': '5'}]
    [Finished in 0.1s]
    13 条回复    2017-09-11 15:06:46 +08:00
    nullcoder
        1
    nullcoder  
       2017-09-07 15:01:47 +08:00
    我推测你是不是用了 jupyte notebook 然后 del 那个 cell 执行了两次
    jmc891205
        2
    jmc891205  
       2017-09-07 15:03:44 +08:00   1
    先不论这样写好坏
    最后一个循环循环了两遍 每遍删除两个 最后不就应该只剩一个吗?
    wiZ90
        3
    wiZ90  
    OP
       2017-09-07 15:04:05 +08:00
    @nullcoder 好像没用。。但是我好想是明白了,我写的逻辑是 c=red 就删除两个切片
    nullcoder
        4
    nullcoder  
       2017-09-07 15:04:42 +08:00   1
    @jmc891205 正解,被排版坑了一眼
    wiZ90
        5
    wiZ90  
    OP
       2017-09-07 15:05:43 +08:00
    @jmc891205 是。。发完我有琢磨了一阵我就明白过来了。但是如何把前两个片删了。。。我再琢磨一会儿吧。。
    wiZ90
        6
    wiZ90  
    OP
       2017-09-07 15:06:45 +08:00
    @nullcoder 奇怪我后台排的是正确的
    TheGonG
        7
    TheGonG  
       2017-09-07 15:16:18 +08:00
    删除前两个?那你不是用 del xx[:2]就是删除了前两个了吗?然后你的代码这样遍历 list 然后再直接判断的去删掉 list 的一部分元素是有问题的。
    awanabe
        8
    awanabe  
       2017-09-07 15:32:02 +08:00
    不要在迭代器中删除本身
    ResidualSoils
        9
    ResidualSoils  
       2017-09-07 15:40:26 +08:00
    不太懂 python,但是你可以反向循环一下试试。
    nullcoder
        10
    nullcoder  
       2017-09-07 15:46:25 +08:00
    初学推荐使用可以断点调试的 VS code IDE
    fhefh
        11
    fhefh  
       2017-09-07 20:40:27 +08:00
    ![]( )
    Heron
        12
    Heron  
       2017-09-11 00:35:49 +08:00
    这排版看的我累死了,不过确实是逻辑问题,循环两次,每次删除两个。最后剩下一个了。
    goofool
        13
    goofool  
       2017-09-11 15:06:46 +08:00
    在迭代器中删除列表的数据很危险
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5818 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 03:28 PVG 11:28 LAX 20:28 JFK 23:28
    Do have faith in what you're doing.
    ubao 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