热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

使用Python创建2D坐标。-UsePythontocreate2Dcoordinate

IamtrulyanoviceinPython.Now,Iamdoingaprojectwhichinvolvescreatingalistof2Dcoordi

I am truly a novice in Python. Now, I am doing a project which involves creating a list of 2D coordinates. The coordinates should be uniformly placed, using a square grid (10*10), like(0,0)(0,1)(0,2)(0,3)...(0,10)(1,0)(1,2)(1,3)...(2,0)(2,1)(2,2)...(10,10).

我真的是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)。

Here is my code:

这是我的代码:

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)

But I can only get: [(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)]

但是我只能得到:[(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)]

How can I rewrite my code to get all the points?

我如何重写代码以得到所有的点?

5 个解决方案

#1


7  

It's common to use a couple of for-loops to achieve this:

使用几个for循环来实现这一点是很常见的:

coordinates = []

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

It's also common to simplify this by flattening it into a list comprehension:

将其简化为列表理解也很常见:

coordinates = [(x,y) for x in range(11) for y in range(11)]

#2


5  

To actually answer your question, you forgot to reset x back to zero after the first run through x=0..9:

为了回答你的问题,你忘了在第一次运行x=0之后将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)

Feel free to use all other variants, of course.

当然,您可以随意使用所有其他变体。

#3


4  

from itertools import product

x = (0, 1, 2)

test = product(x, x)

Result:

结果:

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

Note that test is a generator, so you probably would want to use list(test).

请注意,测试是一个生成器,因此您可能想要使用列表(测试)。

#4


3  

Use itertools.product:

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

The above code is equivalent to this nested list comprehension:

以上代码等价于此嵌套列表理解:

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

#5


1  

Use a for loop. It lets you iterate over things called "iterators". range is a built-in function which returns an iterator from its starting argument (first argument) inclusive. up to its ending argument (second argument) non-inclusive. Thus range(0,11) will return 0,1,2,...,10.

使用一个for循环。它允许您迭代称为“迭代器”的东西。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)

For more information on for loops in Python, check out the official wiki page.

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


推荐阅读
author-avatar
回忆的沙漏2502890423
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有