I need to iterate over a list an arbitrary number of times, yield
ing each element of the list in random order (a different order each time I iterate through the full list). I need to yield each element once before yielding that element a second time, yield each element twice before yielding that element a third time, etc.
Currently, my code looks like this:
def random_yield(data):
random.shuffle(data)
data_index = 0
while True:
yield data[data_index]
data_index += 1
if data_index == len(data):
random.shuffle(data)
data_index = 0
Is there a way to do this more efficiently so I don't pay the performance penalty of the random.shuffle()
after every len(data)
yield
s?