Smarter way of doing this?

Tim Peters at
Thu Feb 5 18:00:46 CET 2004

[Max M]
> ...
> I am interrested in getting a result as a propertion of the
> probablilty, or more correctly in this case, the frequency. If I want
> it as probabilities I just have to normalise the sim to 1.0.
> This has the advantage that the frequencies can be expressed as
> integers too. This is nice in my Markov chain class that count words
> in text, etc.
> In my example below each letter should be occur 50% of the times of
> the previous letter.


> probabilities = [16, 8, 4, 2, 1]
> elements = ['a', 'b', 'c', 'd', 'e']

When the weights are integers, a simpler and faster way to proceed is to
build a list containing each element a number of times equal to its weight:

alist = ['a'] * 16 + ['b'] * 8 + ['c'] * 4 + ['d'] * 2 + ['e']

Then just invoke random.choice on the list.  This is a fast, constant-time
weighted selection technique, but consumes space proportional to the sum of
the weights.

More information about the Python-list mailing list