mercredi 2 septembre 2020

Randomising itertools.combinations

I have a list, l1, which I have generated all combinations of length 2 using itertools.combinations. I intend to loop through these combinations, and perform an operation on them. For simplicity, this code simply prints combination a.

import itertools

l1 = [1,2,3,4,5]
for a in itertools.combinations(l1,2):
    print(a)

Is there any way to randomise the order that the combinations are looped through? random.shuffle does not appear to work, as itertools.combinations has no length.




Aucun commentaire:

Enregistrer un commentaire