mardi 11 mai 2021

How to get random positions of elements with different values in a python list?

Suppose a python list, for instance:l = [1,1,1,2,2,3,3,3,4,4] (not necessarily the list is sorted).

I would like to randomly select positions of n different elements within the list.

For instance, suppose that n=3, so I would like to select the positions of three different elements in the list.

A solution is: positions 0,4,8 respectively related to three different elements values 1,2,4.

What is the most efficient way to do this in Python?




Aucun commentaire:

Enregistrer un commentaire