1. <tfoot id='FkO7v'></tfoot>
        <bdo id='FkO7v'></bdo><ul id='FkO7v'></ul>

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

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

        Python `for` 不会遍历枚举对象

        Python `for` does not iterate over enumerate object(Python `for` 不会遍历枚举对象)

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

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

                  <tbody id='WNN1z'></tbody>
                <legend id='WNN1z'><style id='WNN1z'><dir id='WNN1z'><q id='WNN1z'></q></dir></style></legend>
                <i id='WNN1z'><tr id='WNN1z'><dt id='WNN1z'><q id='WNN1z'><span id='WNN1z'><b id='WNN1z'><form id='WNN1z'><ins id='WNN1z'></ins><ul id='WNN1z'></ul><sub id='WNN1z'></sub></form><legend id='WNN1z'></legend><bdo id='WNN1z'><pre id='WNN1z'><center id='WNN1z'></center></pre></bdo></b><th id='WNN1z'></th></span></q></dt></tr></i><div id='WNN1z'><tfoot id='WNN1z'></tfoot><dl id='WNN1z'><fieldset id='WNN1z'></fieldset></dl></div>
                • <tfoot id='WNN1z'></tfoot>
                  本文介绍了Python `for` 不会遍历枚举对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  为什么不迭代?

                  import logging
                  logging.basicConfig(level=logging.DEBUG)
                  
                  x = []
                  y = [[] for n in range(0, 1)]
                  linedata = ["0","1","2"]
                  x.append( linedata[0] )
                  
                  d = linedata[1:] 
                  logging.debug( "d: {}".format(d) )
                  e = enumerate(d)
                  logging.debug( list(e) )
                  for k, v in e:
                    logging.debug( "k:{} v:{}".format( k, v ) )
                    y[int(k)].append( v )
                    #for d in [(0,1)]:
                    #logging.debug( "k:{} v:{}".format( d[0], d[1] ) )
                    #y[d[0]].append( d[1] )
                  
                  logging.debug( x )
                  logging.debug( y )
                  

                  输出:

                  DEBUG:root:d: ['1', '2']
                  DEBUG:root:[(0, '1'), (1, '2')]
                  DEBUG:root:['0']
                  DEBUG:root:[[]]
                  

                  文档:

                  • https://docs.python.org/3/reference/compound_stmts.html#for
                  • https://docs.python.org/3/library/functions.html#enumerate

                  在线运行:http://goo.gl/75yuAd

                  推荐答案

                  任何迭代器在某种意义上都是一次性的",当它完全执行时,它会变成空的,不能再使用了.当你调用 logging.debug(list(e)) 时,你已经在 list() 函数中使用了它,所以用尽了它.因此,以下在 for 循环中使用它的尝试没有给出任何结果.

                  Any iterator is "one-shot" in sense that, when it is fully executed, it becomes empty and can't be used anymore. When you called logging.debug( list(e) ), you have used it in the list() function and so exhausted it. So, the following attempt to use it in for cycle gives nothing.

                  在此调试后再次调用 enumerate() 时,使用修改后的代码,脚本行为会发生变化 - 它会在 y[int(k)].append( v ) 上引发 IndexError;我不会为你解决这个问题,但这足以表明循环体开始执行.

                  With modified code when enumerate() is called again after this debug, the script behavior gets changed - it raises IndexError on y[int(k)].append( v ); I won't fix this for you but this is enough sign that cycle body begins being executed.

                  这篇关于Python `for` 不会遍历枚举对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 库)

                    <tbody id='Utp29'></tbody>

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

                        <legend id='Utp29'><style id='Utp29'><dir id='Utp29'><q id='Utp29'></q></dir></style></legend>
                        • <bdo id='Utp29'></bdo><ul id='Utp29'></ul>