In python3 range
doesn't return a list but an iterator.
This is great and have some advantages such as not generating the whole list at once but only generating the elements when needed.
Now suppose that I want a range in a random order (basically a shuffled range). In python2 I'd just do:
my_list = range(n)
np.random.shuffle(my_list)
But won't work in python3. Once solution is to generate the actual list and then shuffling (which will then do the same as python2)
my_list = list(range(n))
np.random.shuffle(my_list)
But using this strategy we loose the advantage of the iterator.
Question: How can I create random ordered range iterator that will only give me indexes when requested?
Aucun commentaire:
Enregistrer un commentaire