mardi 15 septembre 2020

How to shuffle a python list such that the occurrence of some elements are preserved?

I start with a list of integers:

A = [ 1, 2, 5, 3, 4, 6, 7, 8 ]

After a shuffle, I would like some elements (say 3,4 and 5) to preserve their occurrence (not location) while the rest are free to be randomly shuffled. Something like:

Outcome #1: A = [ 5, 2, 3, 1, 8, 4, 6, 7 ]

-or-

Outcome #2: A = [ 7, 5, 6, 1, 3, 4, 8, 2 ]

Appreciate all suggestions!




Aucun commentaire:

Enregistrer un commentaire