6

我真的是 Python 的新手。现在,我正在做一个涉及创建二维坐标列表的项目。坐标应均匀放置,使用方格 (10*10),如(0,0)(0,1)(0,2)(0,3)...(0,10)(1,0 )(1,2)(1,3)...(2,0)(2,1)(2,2)...(10,10)。

这是我的代码:

coordinate = []
x = 0
y = 0
while y < 10:
    while x < 10:
        coordinate.append((x,y))
        x += 1
    coordinate.append((x,y))
    y += 1
print(coordinate)

但我只能得到: [(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), ( 7, 0), (8, 0), (9, 0), (10, 0), (10, 1), (10, 2), (10, 3), (10, 4), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9)]

如何重写我的代码以获得所有分数?

4

5 回答 5

9

通常使用几个 for 循环来实现这一点:

coordinates = []

for x in range(11):
  for y in range(11):
    coordinates.append((x, y))

通过将其展平为列表理解来简化这一点也很常见:

coordinates = [(x,y) for x in range(11) for y in range(11)]
于 2013-09-15T20:38:04.940 回答
6
from itertools import product

x = (0, 1, 2)

test = product(x, x)

结果:

>>> for ele in test:
...     print ele
... 
(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
(1, 2)
(2, 0)
(2, 1)
(2, 2)

请注意,这test是一个生成器,因此您可能想要使用list(test).

于 2013-09-15T20:37:44.830 回答
5

x要真正回答您的问题,您在第一次运行 x=0..9 后忘记重置为零:

coordinate = []

y = 0
while y < 10:
    x = 0
    while x < 10:
        coordinate.append((x,y))
        x += 1
    coordinate.append((x,y))
    y += 1
print(coordinate)

当然,请随意使用所有其他变体。

于 2013-09-15T20:38:33.780 回答
4

使用itertools.product

>>> from itertools import product
>>> list(product(range(11), repeat=2))
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (0, 10), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (2, 10), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (4, 10), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (6, 10), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (7, 10), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (8, 10), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9), (9, 10), (10, 0), (10, 1), (10, 2), (10, 3), (10, 4), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9), (10, 10)]

上面的代码等价于这个嵌套列表推导:

>>> [(x, y) for x in range(11) for y in range(11)]
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (0, 10), (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (2, 0), (2, 1), (2, 2), (2, 3), (2, 4), (2, 5), (2, 6), (2, 7), (2, 8), (2, 9), (2, 10), (3, 0), (3, 1), (3, 2), (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (3, 8), (3, 9), (3, 10), (4, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (4, 6), (4, 7), (4, 8), (4, 9), (4, 10), (5, 0), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5), (5, 6), (5, 7), (5, 8), (5, 9), (5, 10), (6, 0), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6), (6, 7), (6, 8), (6, 9), (6, 10), (7, 0), (7, 1), (7, 2), (7, 3), (7, 4), (7, 5), (7, 6), (7, 7), (7, 8), (7, 9), (7, 10), (8, 0), (8, 1), (8, 2), (8, 3), (8, 4), (8, 5), (8, 6), (8, 7), (8, 8), (8, 9), (8, 10), (9, 0), (9, 1), (9, 2), (9, 3), (9, 4), (9, 5), (9, 6), (9, 7), (9, 8), (9, 9), (9, 10), (10, 0), (10, 1), (10, 2), (10, 3), (10, 4), (10, 5), (10, 6), (10, 7), (10, 8), (10, 9), (10, 10)]
于 2013-09-15T20:37:23.743 回答
1

使用for循环。它使您可以迭代称为“迭代器”的事物。range是一个内置函数,它从它的起始参数(第一个参数)返回一个迭代器。直到它的结束参数(第二个参数)不包含在内。因此range(0,11)将返回 0,1,2,...,10。

coordinate = []
for y in range(0, 11):
    for x in range(0, 11):
        coordinate.append((x,y))
print(coordinate)

有关forPython 中循环的更多信息,请查看官方 wiki 页面

于 2013-09-15T20:37:37.690 回答