Merge
This commit is contained in:
commit
c7fac12f19
|
@ -329,6 +329,9 @@ population with repeats::
|
|||
|
||||
>>> weighted_choices = [('Red', 3), ('Blue', 2), ('Yellow', 1), ('Green', 4)]
|
||||
>>> population = [val for val, cnt in weighted_choices for i in range(cnt)]
|
||||
>>> population
|
||||
['Red', 'Red', 'Red', 'Blue', 'Blue', 'Yellow', 'Green', 'Green', 'Green', 'Green']
|
||||
|
||||
>>> random.choice(population)
|
||||
'Green'
|
||||
|
||||
|
@ -338,6 +341,9 @@ with :func:`itertools.accumulate`, and then locate the random value with
|
|||
|
||||
>>> choices, weights = zip(*weighted_choices)
|
||||
>>> cumdist = list(itertools.accumulate(weights))
|
||||
>>> cumdist # [3, 3+2, 3+2+1, 3+2+1+4]
|
||||
[3, 5, 6, 10]
|
||||
|
||||
>>> x = random.random() * cumdist[-1]
|
||||
>>> choices[bisect.bisect(cumdist, x)]
|
||||
'Blue'
|
||||
|
|
Loading…
Reference in New Issue