dimanche 13 septembre 2015

How do I find a random instance (and index) of a certain value in a list in python

I am stuck on a simple way of finding all the instances of a value in a list and randomly selecting one.

I found some stuff on List.index(Value) But it only returns the index of the first instance of that value in my list.

Is there some simple way of returning a list of all the occurrences of the value. Example:

Instances = List.allindexes(Value)  # for example
SelectedIndex = random.choice(Instances)




Aucun commentaire:

Enregistrer un commentaire