Using Python to create a 2D coordinate

I am really new to Python. Now I am doing a project that involves creating a list of 2D coordinates. Coordinates should be evenly spaced using a square grid (10 * 10), e.g. (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 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)]

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

+4
source share
5 answers

To achieve this, several for-loops are usually used:

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

It also usually simplifies this by smoothing it into a list comprehension:

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

To answer your question, you forgot to reset x to return to zero after the first run through 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 options, of course.

+5
source
 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 want to use list(test) .

+4
source

Use 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)] 
+3
source

Use the for loop. This allows you to iterate over things called "iterators." range is a built-in function that returns an iterator from its initial argument (first argument) inclusive. up to its final argument (second argument), not inclusive. Thus, range(0,11) will return 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 page.

+1
source

Source: https://habr.com/ru/post/1502311/


All Articles