Reputation: 81
I am trying to create a function which will give me 70 random coordinate points. This is the code I have now:
# Generate test data
points = []
for _ in range(70):
x = random()
y = random()
points.append((x, y))
print(points)
but the code I have creates an output such as: [(0.4107139467174139, 0.3864211980981259)] [(0.4107139467174139, 0.3864211980981259), (0.6159949847158482, 0.6526570064673685)] [(0.4107139467174139, 0.3864211980981259), (0.6159949847158482, 0.6526570064673685), (0.6515694521853568, 0.9651948000023821)] ...
I have also tried:
# Generate test data
cord_set = []
for _ in range(70):
x = random()
y = random()
cord_set.append((x, y))
print(cord_set)
With similar results.
Upvotes: 0
Views: 1606
Reputation: 1290
In the comment section, you mentioned that you just want an output like (x1, y1), (x2, y2), the reason why you are getting this many of list is because in the loop, you print the list points in every iteration. Just remove the print statement out of the loop should give you the output you want.
points = []
for _ in range(70):
x = random()
y = random()
points.append((x, y))
print(points)
Based on your comment, I think you are saying for every iteration, you want the result to be [(x,y)] which is a list. If so, you need to have a temp list to pass those generated value:
points = []
for _ in range(70):
single_point = []
x = random()
y = random()
single_point.append((x, y))
points.append(single_point)
print(points)
This gives you [[(x1,y1)],[(x2,y2)],...]
If that is the case, I don't think you will need the () to make it nested. You can just go with:
points = []
for _ in range(70):
single_point = []
x = random()
y = random()
single_point.append(x)
single_point.append(y)
points.append(single_point)
print(points)
This gives you [[x1,y1],[x2,y2]...]
Upvotes: 1