Pick items from list with probability based upon property of list member ?

Stefan Behnel stefan_ml at behnel.de
Sun Jun 20 06:44:30 EDT 2010


southof40, 20.06.2010 12:19:
> I have list of of N Vehicle objects - the only possible vehicles are
> cars, bikes, trucks.
>
> I want to select an object from the list with a probability of : cars
> 0.7, bikes 0.3, trucks 0.1.
>
> I've currently implemented this by creating another list in which each
> car object from the original list appears 7 times, each bike 3 times
> and each truck once. I then pick at random from that list.
>
> This works but seems very clunky to me.

Why? It's a very simple, generic, easy to understand and fast solution to 
the problem.

Stefan




More information about the Python-list mailing list