1

I want to create a large list containing 20,000 points in the form of:

[[x, y], [x, y], [x, y]]

where x and y can be any random integer between 0 and 1000. How would I be able to do this such that there are no duplicate coordinates [x, y]?

leekaiinthesky
  • 5,413
  • 4
  • 28
  • 39
Jack8246
  • 153
  • 1
  • 3
  • 10
  • you need random elements in the list ? – ZdaR Jun 03 '15 at 04:31
  • try: http://stackoverflow.com/questions/6499327/is-there-python-way-to-generate-pairs – lnNoam Jun 03 '15 at 04:32
  • As you create a coordinate pair compare back through the list to see if it exists before appending it... Are they integers? – Michael Stimson Jun 03 '15 at 04:33
  • Yes integers, i have tried appending it to the list and then checking, but it will freeze because i think it might be too slow when it has to check the list if it is there evey time? – Jack8246 Jun 03 '15 at 04:35
  • @InNoam thanks i will give that a try – Jack8246 Jun 03 '15 at 04:35
  • get your [X, Y] as random(0,1000) and *before* appending use *if not [x,y] in MainList: MainList.append([x,y])* the indexing function is faster than comparing each preceding element in python. – Michael Stimson Jun 03 '15 at 04:41

4 Answers4

2

You could just use a while loop to pad it out until it's big enough:

>>> from random import randint
>>> n, N = 1000, 20000
>>> points = {(randint(0, n), randint(0, n)) for i in xrange(N)}
>>> while len(points) < N:
...     points |= {(randint(0, n), randint(0, n))}
...     
>>> points = list(list(x) for x in points)

Your initial idea was probably slow because it was iterating lists for checking containmentship, which is O(n). This uses sets which are faster, and then only converts to the list structure once at the end.

wim
  • 338,267
  • 99
  • 616
  • 750
1

Try this :

import itertools
x = range(0,10)
aList =[]
for pair in itertools.combinations(x,2):
    for i in range(0,10):
        aList.append(pair)
print aList

If you want point between 0-10 with all unique and stored in a list, or you You need it random order, then use some random function .

coder3521
  • 2,608
  • 1
  • 28
  • 50
0

Since n = 1001 is relatively small in your case, random.sample(population, k) will do just fine, taking a random sample of 20000 pairs from the space of possible pairs (no duplicates):

import random
print random.sample([[x, y] for x in xrange(1001) for y in xrange(1001)], 20000)

This is the most concise and readable solution. (But if n is very big, generating the entire space of points will not be computationally efficient.)

leekaiinthesky
  • 5,413
  • 4
  • 28
  • 39
0

An approach that avoids while loops with unknown iteration counts and avoids storing huge lists in memory is to use random.sample to produce unique encoded values from a single range (in Py3) or xrange (in Py2) to avoid actually generating huge temporaries; a simple mathematical operation can split the "encoded" values back into two values:

import random
xys = random.sample(range(1001 * 1001), 20000)
[divmod(xy, 1001) for xy in xys] # Wrap divmod in list() if you must have list, not tuple
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271