Python `for` does not iterate over enumerate object

前端 未结 4 1789
日久生厌
日久生厌 2021-01-28 02:56

Why does this not iterate?

import logging
logging.basicConfig(level=logging.DEBUG)

x = []
y = [[] for n in range(0, 1)]
linedata = [\"0\",\"1\",\"2\"]
x.append(         


        
4条回答
  •  抹茶落季
    2021-01-28 03:45

    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.

    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.

提交回复
热议问题