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:
Using numpy
def choice(items, weights): return items[np.argmin((np.cumsum(weights) / sum(weights)) < np.random.rand())]