<tfoot id='4UluK'></tfoot>
  1. <small id='4UluK'></small><noframes id='4UluK'>

    <legend id='4UluK'><style id='4UluK'><dir id='4UluK'><q id='4UluK'></q></dir></style></legend>
    <i id='4UluK'><tr id='4UluK'><dt id='4UluK'><q id='4UluK'><span id='4UluK'><b id='4UluK'><form id='4UluK'><ins id='4UluK'></ins><ul id='4UluK'></ul><sub id='4UluK'></sub></form><legend id='4UluK'></legend><bdo id='4UluK'><pre id='4UluK'><center id='4UluK'></center></pre></bdo></b><th id='4UluK'></th></span></q></dt></tr></i><div id='4UluK'><tfoot id='4UluK'></tfoot><dl id='4UluK'><fieldset id='4UluK'></fieldset></dl></div>
      • <bdo id='4UluK'></bdo><ul id='4UluK'></ul>
    1. 在使用 python 迭代列表时修改列表

      Modifying a list while iterating over it with python(在使用 python 迭代列表时修改列表)
        <tfoot id='0suhW'></tfoot>
          <tbody id='0suhW'></tbody>
          <bdo id='0suhW'></bdo><ul id='0suhW'></ul>
          <legend id='0suhW'><style id='0suhW'><dir id='0suhW'><q id='0suhW'></q></dir></style></legend>

            <i id='0suhW'><tr id='0suhW'><dt id='0suhW'><q id='0suhW'><span id='0suhW'><b id='0suhW'><form id='0suhW'><ins id='0suhW'></ins><ul id='0suhW'></ul><sub id='0suhW'></sub></form><legend id='0suhW'></legend><bdo id='0suhW'><pre id='0suhW'><center id='0suhW'></center></pre></bdo></b><th id='0suhW'></th></span></q></dt></tr></i><div id='0suhW'><tfoot id='0suhW'></tfoot><dl id='0suhW'><fieldset id='0suhW'></fieldset></dl></div>

            1. <small id='0suhW'></small><noframes id='0suhW'>

                本文介绍了在使用 python 迭代列表时修改列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                当我想修改原始列表时,我知道要遍历列表的副本.然而,我收到的关于在迭代列表时修改列表有什么问题的唯一解释是它可能导致意想不到的结果".

                I know to iterate over a copy of my list when I want to modify the original. However, the only explanation I've ever received on what's wrong with modifying a list while iterating over it is that "it can lead to unexpected results."

                考虑以下几点:

                lst = ['a', 'b', 'c', 'd', 'e']
                for x in lst:
                    lst.remove(x)
                print(lst)
                

                这是我试图解释在迭代列表时修改列表时实际发生的情况.注意 line2 等价于 for i in range(len(lst)):,并且 len(lst) 每次迭代都会减 1.

                Here is my attempt at explaining what actually happens when one modifies a list while iterating over it. Note that line2 is equivalent to for i in range(len(lst)):, and that len(lst) decreases by 1 with every iteration.

                len(lst) 以 5 开头.

                i = 0 时,我们有 lst[i] = 'a' 被移除,所以 lst = ['b', 'c','d', 'e'].len(lst) 减少到 4.

                When i = 0, we have lst[i] = 'a' being removed, so lst = ['b', 'c', 'd', 'e']. len(lst) decreases to 4.

                i = 1 时,我们有 lst[i] = 'c' 被移除,所以 lst = ['b', 'd','e']len(lst) 减少到 3.

                When i = 1, we have lst[i] = 'c' being removed, so lst = ['b', 'd', 'e'] len(lst) decreases to 3.

                i = 2 时,我们会移除 lst[i] = 'e',所以 lst = ['b', 'd'].len(lst) 减少到 2.

                When i = 2, we have lst[i] = 'e' being removed, so lst = ['b', 'd']. len(lst) decreases to 2.

                这是我认为会引发 IndexError 的地方,因为 i = 2 不在 range(2) 中.然而,程序只是简单地输出 ['b', 'd'].是因为 i 已经赶上"了 len(lst) 吗?另外,到目前为止我的推理是否合理?

                This is where I thought an IndexError would be raised, since i = 2 is not in range(2). However, the program simply outputs ['b', 'd']. Is it because i has "caught up" with len(lst)? Also, is my reasoning sound so far?

                推荐答案

                C实现在listiter_next函数中blob/master/Objects/listobject.c" rel="nofollow noreferrer">listobject.c 相关行是

                The C implementation is in the listiter_next function in listobject.c and the pertinent lines are

                if (it->it_index < PyList_GET_SIZE(seq)) {
                    item = PyList_GET_ITEM(seq, it->it_index);
                    ++it->it_index;
                    Py_INCREF(item);
                    return item;
                }
                
                it->it_seq = NULL;
                Py_DECREF(seq);
                return NULL;
                

                如果对象仍在范围内 (it->it_index < PyList_GET_SIZE(seq)),则迭代器返回一个对象,否则返回 NONE.不管你偏离 1 还是 100 万,这都不是错误.

                The iterator returns an object if its still in range (it->it_index < PyList_GET_SIZE(seq)) and returns NONE otherwise. It doesn't matter if you are off by 1 or a million, its not an error.

                这样做的一般原因是迭代器和可迭代对象可以在多个地方使用(考虑在 for 循环中读取的文件对象).外循环不应该仅仅因为没有事情要做就因为 IndexError 而崩溃.更改您正在迭代的对象并不违法或本质上愚蠢",只是您需要知道您的行为的后果.

                The general reason for doing things this way is that iterators and iterables can be consumed in multiple places (consider a file object that is read inside a for loop). An outer loop shouldn't crash with an IndexError just because its run out of things to do. Its not illegal or inherently "stupid" to change an object you are iterating, its just that you need to know the consequences of your actions.

                这篇关于在使用 python 迭代列表时修改列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

                • <i id='iDRzf'><tr id='iDRzf'><dt id='iDRzf'><q id='iDRzf'><span id='iDRzf'><b id='iDRzf'><form id='iDRzf'><ins id='iDRzf'></ins><ul id='iDRzf'></ul><sub id='iDRzf'></sub></form><legend id='iDRzf'></legend><bdo id='iDRzf'><pre id='iDRzf'><center id='iDRzf'></center></pre></bdo></b><th id='iDRzf'></th></span></q></dt></tr></i><div id='iDRzf'><tfoot id='iDRzf'></tfoot><dl id='iDRzf'><fieldset id='iDRzf'></fieldset></dl></div>
                    <tbody id='iDRzf'></tbody>
                    <bdo id='iDRzf'></bdo><ul id='iDRzf'></ul>

                  • <small id='iDRzf'></small><noframes id='iDRzf'>

                      1. <tfoot id='iDRzf'></tfoot>
                          <legend id='iDRzf'><style id='iDRzf'><dir id='iDRzf'><q id='iDRzf'></q></dir></style></legend>