从列表中获取值时,同一个函数不会获取数据两次?(python)

When getting values from a list ,the same function doesn't fetch data twice?(python)

>>> movement = ((x,y)for x in range(-1,2) for y in range (-1,2) if (x,y)!=(0,0))
>>> for x,y in movement:
        print x,y


-1 -1
-1 0
-1 1
0 -1
0 1
1 -1
1 0
1 1
>>> for a,b in movement:
        print a,b


>>> for x,y in movement:
        print x,y


>>>

终端和空闲都发生了这种情况(我做错了什么还是应该发生?)

您正在创建一个 generator 对象 (https://wiki.python.org/moin/Generators)。这意味着您可以精确地迭代一次 - 这就是为什么它第一次像宣传的那样工作。

将运动 'maker' 改成这个,它起作用了;现在它创建一个 list,而不是 generator.

movement = [(x,y)for x in range(-1,2) for y in range (-1,2) if (x,y)!=(0,0)]

详细一点;在链接页面上是这样说的:

In fact, we can turn a list comprehension into a generator expression by replacing the square brackets ("[ ]") with parentheses.