Itertools 对象在 python 脚本运行后消失
Itertools object vanishes after the python script runs
我有一个 python 脚本,我在其中一次执行以下操作:
iterator = product(*izip(repeat(0,len(myList)),myList))
我使用 python 命令行,并执行 execfile('myScript.py')
。在代码有 运行 之后,我尝试以下操作:
>>> iterator
<itertools.product object at 0x182f06370>
>>> list(iterator)
[]
但是,如果我在生成 iterator
之后立即在脚本 print list(iterator)
中放置打印语句,代码将正确打印列表的元素。
为什么文件执行完后元素消失了?这是正常行为吗?
来自python docs:
Iterator is an object representing a stream of data. Repeated calls to the
iterator’s next() method return successive items in the stream. When
no more data are available a StopIteration exception is raised
instead. At this point, the iterator object is exhausted and any
further calls to its next() method just raise StopIteration again.
我有一个 python 脚本,我在其中一次执行以下操作:
iterator = product(*izip(repeat(0,len(myList)),myList))
我使用 python 命令行,并执行 execfile('myScript.py')
。在代码有 运行 之后,我尝试以下操作:
>>> iterator
<itertools.product object at 0x182f06370>
>>> list(iterator)
[]
但是,如果我在生成 iterator
之后立即在脚本 print list(iterator)
中放置打印语句,代码将正确打印列表的元素。
为什么文件执行完后元素消失了?这是正常行为吗?
来自python docs:
Iterator is an object representing a stream of data. Repeated calls to the iterator’s next() method return successive items in the stream. When no more data are available a StopIteration exception is raised instead. At this point, the iterator object is exhausted and any further calls to its next() method just raise StopIteration again.