• <tfoot id='qAv02'></tfoot>

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

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

        • <bdo id='qAv02'></bdo><ul id='qAv02'></ul>

        <i id='qAv02'><tr id='qAv02'><dt id='qAv02'><q id='qAv02'><span id='qAv02'><b id='qAv02'><form id='qAv02'><ins id='qAv02'></ins><ul id='qAv02'></ul><sub id='qAv02'></sub></form><legend id='qAv02'></legend><bdo id='qAv02'><pre id='qAv02'><center id='qAv02'></center></pre></bdo></b><th id='qAv02'></th></span></q></dt></tr></i><div id='qAv02'><tfoot id='qAv02'></tfoot><dl id='qAv02'><fieldset id='qAv02'></fieldset></dl></div>
      1. Python迭代器在对其执行某些操作后为空

        Python iterator is empty after performing some action on it(Python迭代器在对其执行某些操作后为空)

        • <legend id='SELY3'><style id='SELY3'><dir id='SELY3'><q id='SELY3'></q></dir></style></legend>

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

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

                  <tbody id='SELY3'></tbody>
                • <small id='SELY3'></small><noframes id='SELY3'>

                • 本文介绍了Python迭代器在对其执行某些操作后为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我试图在 python3 中对 codeeval 进行挑战,但在试图改进我的解决方案时遇到了困难.每次我尝试在同一个迭代器上连续两次迭代(或打印或其他操作)时,第二个循环都是空的.这是一个产生这种行为的最小示例,尽管我尝试了几种与列表等不同的组合,但结果相同:

                  I was trying to do a challenge on codeeval in python3 and got stuck trying to improve my solution. Every time i tried to iterate (or print, or some other action) two times consecutively over the same iterator, the second loop came up empty. Here is a minimal example that produces this behavior, although I tried several different combinations with lists etc. that gave me the same result:

                  numbers = ('1','2','3','4','5')
                  numbers = map(int, numbers)                                                    
                  print(list(numbers))                                                          
                  print(list(numbers))
                  

                  结果:

                  [1, 2, 3, 4, 5]
                  []
                  

                  为什么 print(在这种情况下)会删除数字的内容?

                  Why does print (in this case) delete the content of numbers?

                  推荐答案

                  这正是迭代器的工作方式.它们的设计目的是只生成一次动态数据,不再重复.如果您想再次从中获取数据,您要么必须将所有迭代数据保存到另一个列表,要么再次启动迭代器.如果您需要从文件中读取数据或执行其他烦人的操作以重新获取该迭代器,最好在生成数据时将该数据存储在列表中.

                  This is exactly how iterators work. They're designed to generate data on the fly exactly one time, no more. If you want to get data out of it a second time, you either have to save all the iterated data to another list, or initiate the iterator again. In cases where you need to read from files or do other annoying things to re-obtain that iterator, it's probably best to just store that data in a list when it's generated.

                  >>> numbers = ('1','2','3','4','5')
                  >>> ints = [x for x in map(int, numbers)]
                  >>> print(list(ints))
                  [1, 2, 3, 4, 5]
                  >>> print(list(ints))
                  [1, 2, 3, 4, 5]
                  

                  https://docs.python.org/2/library/stdtypes.html#iterator-types

                  协议的意图是一旦迭代器的 next() 方法引发 StopIteration,它将在后续调用中继续这样做.不遵守此属性的实现被视为已损坏.(此约束是在 Python 2.3 中添加的;在 Python 2.2 中,各种迭代器根据此规则被破坏.)

                  The intention of the protocol is that once an iterator’s next() method raises StopIteration, it will continue to do so on subsequent calls. Implementations that do not obey this property are deemed broken. (This constraint was added in Python 2.3; in Python 2.2, various iterators are broken according to this rule.)

                  我应该注意,我在 Python 2.4.3 上运行了您提供的确切代码,并且每次都打印出列表.所以这是一个版本依赖的事情,我想.

                  I should note that I ran the exact code you gave on Python 2.4.3, and it printed out the list every time. So it's a version dependent thing, I suppose.

                  这篇关于Python迭代器在对其执行某些操作后为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Split a Pandas column of lists into multiple columns(将 Pandas 的列表列拆分为多列)
                  How does the @property decorator work in Python?(@property 装饰器在 Python 中是如何工作的?)
                  What is the difference between old style and new style classes in Python?(Python中的旧样式类和新样式类有什么区别?)
                  How to break out of multiple loops?(如何打破多个循环?)
                  How to put the legend out of the plot(如何将传说从情节中剔除)
                  Why is the output of my function printing out quot;Nonequot;?(为什么我的函数输出打印出“无?)
                • <tfoot id='BjhY8'></tfoot>
                    <legend id='BjhY8'><style id='BjhY8'><dir id='BjhY8'><q id='BjhY8'></q></dir></style></legend>
                      <bdo id='BjhY8'></bdo><ul id='BjhY8'></ul>

                        <tbody id='BjhY8'></tbody>

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