<small id='sT8MI'></small><noframes id='sT8MI'>

        • <bdo id='sT8MI'></bdo><ul id='sT8MI'></ul>
        <tfoot id='sT8MI'></tfoot>

        <legend id='sT8MI'><style id='sT8MI'><dir id='sT8MI'><q id='sT8MI'></q></dir></style></legend>

        <i id='sT8MI'><tr id='sT8MI'><dt id='sT8MI'><q id='sT8MI'><span id='sT8MI'><b id='sT8MI'><form id='sT8MI'><ins id='sT8MI'></ins><ul id='sT8MI'></ul><sub id='sT8MI'></sub></form><legend id='sT8MI'></legend><bdo id='sT8MI'><pre id='sT8MI'><center id='sT8MI'></center></pre></bdo></b><th id='sT8MI'></th></span></q></dt></tr></i><div id='sT8MI'><tfoot id='sT8MI'></tfoot><dl id='sT8MI'><fieldset id='sT8MI'></fieldset></dl></div>
      1. Python:在迭代时将元素添加到列表中

        Python: Adding element to list while iterating(Python:在迭代时将元素添加到列表中)
          <bdo id='hz1eE'></bdo><ul id='hz1eE'></ul>
          <tfoot id='hz1eE'></tfoot>
            <tbody id='hz1eE'></tbody>
          <i id='hz1eE'><tr id='hz1eE'><dt id='hz1eE'><q id='hz1eE'><span id='hz1eE'><b id='hz1eE'><form id='hz1eE'><ins id='hz1eE'></ins><ul id='hz1eE'></ul><sub id='hz1eE'></sub></form><legend id='hz1eE'></legend><bdo id='hz1eE'><pre id='hz1eE'><center id='hz1eE'></center></pre></bdo></b><th id='hz1eE'></th></span></q></dt></tr></i><div id='hz1eE'><tfoot id='hz1eE'></tfoot><dl id='hz1eE'><fieldset id='hz1eE'></fieldset></dl></div>

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

                  <legend id='hz1eE'><style id='hz1eE'><dir id='hz1eE'><q id='hz1eE'></q></dir></style></legend>

                  本文介绍了Python:在迭代时将元素添加到列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我知道在迭代列表时不允许删除元素,但是否允许在迭代时将元素添加到 python 列表中.这是一个例子:

                  I know that it is not allowed to remove elements while iterating a list, but is it allowed to add elements to a python list while iterating. Here is an example:

                      for a in myarr:
                        if somecond(a):
                            myarr.append(newObj())
                  

                  我已经在我的代码中尝试过,它似乎工作正常,但我不知道是不是因为我很幸运,它会在未来的某个时候中断?

                  I have tried this in my code and it seems to work fine, however I don't know if it's because I am just lucky and that it will break at some point in the future?

                  我不想复制列表,因为myarr"很大,因此会太慢.我还需要用somecond()"检查附加的对象.

                  I prefer not to copy the list since "myarr" is huge, and therefore it would be too slow. Also I need to check the appended objects with "somecond()".

                  在某些时候somecond(a)"会是假的,所以不可能有无限循环.

                  At some point "somecond(a)" will be false, so there can not be an infinite loop.

                  有人询问somecond()"函数.myarr 中的每个对象都有一个大小,每次 "somecond(a)" 为真并且将一个新对象附加到列表中时,新对象的大小都会小于 a.somecond()" 有一个 epsilon 来表示对象可以有多小,如果它们太小,它将返回false"

                  Someone asked about the "somecond()" function. Each object in myarr has a size, and each time "somecond(a)" is true and a new object is appended to the list, the new object will have a size smaller than a. "somecond()" has an epsilon for how small objects can be and if they are too small it will return "false"

                  推荐答案

                  您可以使用 itertools 中的 islice 在列表的一小部分上创建迭代器.然后,您可以将条目附加到列表中,而不会影响您正在迭代的项目:

                  You could use the islice from itertools to create an iterator over a smaller portion of the list. Then you can append entries to the list without impacting the items you're iterating over:

                  islice(myarr, 0, len(myarr)-1)
                  

                  更好的是,您甚至不必遍历所有元素.您可以增加步长.

                  Even better, you don't even have to iterate over all the elements. You can increment a step size.

                  这篇关于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 库)

                  <small id='KpAVG'></small><noframes id='KpAVG'>

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

                    1. <tfoot id='KpAVG'></tfoot>

                        <bdo id='KpAVG'></bdo><ul id='KpAVG'></ul>