6

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

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

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jianli Cheng
  • 371
  • 1
  • 8
  • 17
  • for future reference to anybody stumbling across this, his problem stemmed from not resetting x = 0 at the start of the loop! – J_mie6 Mar 02 '15 at 17:33

5 Answers5

8

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

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)]
erewok
  • 7,555
  • 3
  • 33
  • 45
  • The problem with this approach is that `coordinates[j]` refers to the j-th coordinate -- not the j-th coordinate tuple: e.g. `coordinates[11]` is `(1,0)` instead of `(10, 10)`. – AltShift Dec 12 '18 at 21:31
  • Here is a solution that uses a class to define coordinate pairs as points: https://stackoverflow.com/questions/12468900/making-a-point-class-in-python . – AltShift Dec 12 '18 at 21:37
6
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).

Akavall
  • 82,592
  • 51
  • 207
  • 251
5

To actually answer your question, you forgot to reset x back 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 variants, of course.

Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
  • Great!!! It works! Thank you so much. But I have another question: how can I make my screen output without comma and parenthesis? like 0 0;0 1;0 2, etc – Jianli Cheng Sep 15 '13 at 21:19
  • You'll have to loop over `coordinate` and format your output yourself. Start by reading up on `for` loops, try printing each pair individually. Post another question if you encounter problems. – Pavel Anossov Sep 15 '13 at 21:29
4

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)]
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
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.

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.

Shashank
  • 13,713
  • 5
  • 37
  • 63
  • Thanks for your reply. Also,how can I make my screen output without comma and parenthesis? like 0 0;0 1;0 2, not (0,0)(0,1)(0,2). Thanks – Jianli Cheng Sep 15 '13 at 21:29