I have a list:
a = [1,2,1,1,3,5,6,2] I want to select, say 3 elements at random from this list, but they must all be different.
I need to preserve the 'weight' of each element, so sampling from set(a) is not possible.
So far, my solution is:
while condition == False: mysample = random.sample(a, 3) if len(set(mysample)) - len(mysample) !=0: condition = False else: condition = True But this forces me to re-sample as many times as it takes for the elements to all be different. This works fine for small sampling, but for large sampling, my code becomes very inefficient...
random.sample(a, 3)yielded[1, 1, 6]on my machine. It returned two of the three1s in the list.