How to fill an array in python without repetitions?

def generate():
 global dot
 dot.append([random.randrange(0, dots), random.randrange(0, dots)])
 while len(dot)<dots-1: randx="random.randrange(0," dots) randy="random.randrange(0," for i in range(len(dot)): if dot[i][0]!="randX" and dot[i][1]!="randY:" dot.append([random.randrange(0, dots), random.randrange(0, dots)])< code></dots-1:>
August 19th 19 at 23:10
3 answers
August 19th 19 at 23:12
The decision vlob
In [3]: from random import randint

In [4]: def t(list_len=10):
 list_ = []
 while len(list_) < list_len:
 r = randint(0, list_len * 10)
 if r not in list_: list_.append(r)
 return list_

In [5]: t()
Out[5]: [34, 21, 49, 40, 81, 78, 95, 63, 31, 29]

In [6]: t(20)
there each element in the list.

Your option is generally easier to solve using set

from random import randint
def t (list_len=10):
list_ = set()
while len(list_) < list_len
list_.add(randint(0, list_len * 10))
return list(list_)

ps indentation think it is clear where. - barrett_Kohler commented on August 19th 19 at 23:15
August 19th 19 at 23:14
In the face prettier, I think, not to solve it. But you can replace the list of two randomness on the tuple, and the list of lists - least at the time of filling.
Get a little nicer (at least from my point of view) :

from random import randint

def generate(dots):
 a = set()
 while len(a) < dots:
 a.add((randint(0, dots), randint(1, dots)))
 return list(a)

If you still a list of lists, then the last line replace with
return [list(i) for i in list(a)]
August 19th 19 at 23:16
Simple and much faster
def generate(dots):
 dot = list(range(dots))
 for i in range(dots):
 x = random.randrange(0, dots)
 dot[i] , dot[x] = dot[x], dot[i]
 return dot

dot = generate(dots)
In the task list of lists...
you can make zip(generate(dots),generate(dots)) - get a list of tuples
or map(lambda x,y:[x,y],generate(dots),generate(dots)) - will be a list of lists - barrett_Kohler commented on August 19th 19 at 23:19
: the tag code it is possible and better to use - mara.Doyle commented on August 19th 19 at 23:22

Find more questions by tags Python