Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Python to create 2D coordinate

Tags:

python

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?

like image 734
Jianli Cheng Avatar asked Sep 15 '13 20:09

Jianli Cheng


People also ask

How to draw a line with two coordinates in Matplotlib?

Here plot method is used for plotting and the show is for showing output to the user. Now if there are given two coordinates to draw a line, firstly we have to make two different arrays for the values of x and y respectively and use that x and y value array in the plot function of matplotlib to draw the line for the corresponding coordinates.

How to find the area using the coordinates of vertices in Python?

We will pass on a function inside the function to find the area using the coordinates of the vertices. Follow the code to define the function in python. Code Explanation: Just like how we did in the previous step, we are defining a function ‘find_triangle_area’ that takes the coordinates of vertices as parameters.

How do I get the coordinates of a range in Python?

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.

How to create 2D pixel plots in Python using matplotlib?

The basic steps to create 2D pixel plots in python using Matplotlib are as follows: We are importing NumPy library for creating a dataset and a ‘pyplot’ module from a matplotlib library for plotting pixel plots For plotting, we need 2-dimensional data. Let’s create a 2d array using the random method in NumPy.


1 Answers

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)]
like image 64
erewok Avatar answered Oct 21 '22 19:10

erewok