【发布时间】:2012-07-02 00:57:18
【问题描述】:
我被困在这段代码上,因为我无法让生成器在每次调用它时都返回下一个值——它只是停留在第一个值上!看看:
从 numpy 导入 *
def ArrayCoords(x,y,RowCount=0,ColumnCount=0): # I am trying to get it to print
while RowCount<x: # a new coordinate of a matrix
while ColumnCount<y: # left to right up to down each
yield (RowCount,ColumnCount) # time it's called.
ColumnCount+=1
RowCount+=1
ColumnCount=0
这是我得到的:
>>> next(ArrayCoords(20,20))
... (0, 0)
>>> next(ArrayCoords(20,20))
... (0, 0)
但它只是停留在第一个!我预料到了:
>>> next(ArrayCoords(20,20))
... (0, 0)
>>> next(ArrayCoords(20,20))
... (0, 1)
>>> next(ArrayCoords(20,20))
... (0, 2)
你们能帮我写代码并解释为什么会这样吗? 提前谢谢!
【问题讨论】: