24
加权版本的random.choice
我需要写一个加权版本的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 …
244
python
optimization