我需要编写random.choice的加权版本(列表中的每个元素都有不同的被选中概率).这就是我想出的:
def weightedChoice(choices):
    """Like random.choice, but each element can have a different chance of
    being selected.
    choices can be any iterable containing iterables with two items each.
    Technically, they can have more than two items, the rest will just be
    ignored.  The first item is the thing being chosen, the second item is
    its weight.  The weights can be any numeric values, what matters is the
    relative differences between them.
    """
    space = {}
    current = 0
    for choice, weight …