mercredi 16 décembre 2020

How to shuffle an iterator of known size in Python? [duplicate]

I am trying to bijectively map a range of indices to another set of indices. So I came up with this:

def get_index(i):
    # Does more complicated stuff, but just to illustrate.
    return 2*i + 1

my_iterator = iter(get_index(i) for i in range(100))

# then I use my_iterator
for a in my_iterator:
    ...

Now I would like to provide a randomly permuted version of this iterator (without repetition) and my question is how to do that without having to explicit the range into a list. The number of elements is huge and I would not like to store the whole thing in memory.




Aucun commentaire:

Enregistrer un commentaire