I needed to write a weighted version of random.choice (each element in the list has a different probability for being selected). This is what I came up with:
import random
def weighted_choice(choices, weights):
total = sum(weights)
treshold = random.uniform(0, total)
for k, weight in enumerate(weights):
total -= weight
if total < treshold:
return choices[k]