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 · 2498 次点击
    这是一个创建于 2395 天前的主题,其中的信息可能已经有所发展或是发生改变。
    书上习题写完了没事儿写着玩,问题在第 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 奇怪我后台排的是正确的
    Daath
        7
    Daath  
       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   ·   我们的愿景   ·   实用小工具   ·   2855 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:40 · PVG 21:40 · LAX 06:40 · JFK 09:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.