【问题标题】:Use Python to create 2D coordinate使用 Python 创建二维坐标
【发布时间】:2013-09-19 23:09:24
【问题描述】:

我确实是 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)]

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

【问题讨论】:

  • 供将来遇到此问题的任何人参考,他的问题源于未在循环开始时重置 x = 0!

标签: python


【解决方案1】:
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)

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      要真正回答您的问题,您忘记在第一次运行 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 等
      • 您必须遍历coordinate 并自己格式化输出。从阅读for 循环开始,尝试单独打印每一对。如果您遇到问题,请发布另一个问题。
      【解决方案4】:

      通常使用几个 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)
      • 以下是使用类来定义坐标对作为要点的解决方案:stackoverflow.com/questions/12468900/…。 span>
      【解决方案5】:

      使用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 循环的更多信息,请查看official wiki page

      【讨论】:

      • 感谢您的回复。另外,我怎样才能使我的屏幕输出没有逗号和括号?像 0 0;0 1;0 2,而不是 (0,0)(0,1)(0,2)。谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-02
      • 2022-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多