使用Python创建二维坐标系

6

我在Python方面真的是个新手。现在,我正在做一个涉及创建2D坐标列表的项目。这些坐标应该是均匀分布的,使用一个正方形网格(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)]
我应该如何重写代码以获取所有点?

对于未来可能遇到此问题的任何人,请注意,他的问题源于在循环开始时未重置 x = 0! - J_mie6
5个回答

8

通常使用一对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)]

这种方法的问题在于 coordinates[j] 是指第 j 个坐标,而不是第 j 个坐标元组:例如,coordinates[11](1,0) 而不是 (10, 10) - AltShift
这里有一个解决方案,它使用一个类来定义坐标对作为点:https://dev59.com/1GjWa4cB1Zd3GeqPro7V。 - AltShift

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)

5

实际上回答你的问题,第一次运行 x=0..9 后,您忘记将 x 重置为零:

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)

当然,你可以随意使用其他变体。


太好了!它起作用了!非常感谢。但我还有一个问题:如何使我的屏幕输出不带逗号和括号?像0 0;0 1;0 2这样。 - Jianli Cheng
你需要循环遍历coordinate并自己格式化输出。首先阅读有关for循环的内容,尝试逐个打印每对坐标。如果遇到问题,请发布另一个问题。 - Pavel Anossov

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)]

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)

如需了解Python中的for循环更多信息,请查看官方维基页面


谢谢您的回复。另外,我该如何使我的屏幕输出不带逗号和括号?例如0 0;0 1;0 2,而不是(0,0)(0,1)(0,2)。谢谢。 - Jianli Cheng

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接