0

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.

2
  • Ok, what don't you like about what it's generating? What's your expected range? And which random() are you even calling? The title of your question isn't even a question - try rephrasing it to state what specifically is wrong. See How to Ask Commented Oct 16, 2021 at 2:16
  • @msanford I want an output such as (x1, y1), (x2, y2), etc. It doesn't have to be within a specific range. Commented Oct 16, 2021 at 2:19

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Just wait till he figures out he's getting the same 2 points over and over...
@Zichezeng Unfortunately I need the output to look like: [(x1, y1)] [(x2, y2][(x3, y3)] ... instead of [(x1, y1)] [(x2, y2), (x3, y3)] [(x4, y4), (x5, y5), (x6, x6)]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.