mardi 2 mars 2021

Special shuffling of the array

I want to shuffle my numpy array a = [2, 2, 2, 1, 1] in this way: a = [2, 1, 2, 1, 2]. So that the same elements do not stand side by side if possible. I know about numpy.array.shuffle but it generates all possible permutations uniformly. Therefore, with the same probability, can appear a = [2, 1, 2, 1, 2] or a = [2, 2, 2, 1, 1]. Is there vectorised solution for more difficult arrays? For example, for this b = np.hstack([np.ones(101), np.ones(50) * 2, np.ones(20) * 3]) array.




Aucun commentaire:

Enregistrer un commentaire