Ich habe ein Liste von Elementen, welche beispielsweise so gegliedert ist:
Code: Alles auswählen
seq = [("A", 0.25), ("B", 0.25), ("C", 0.2), ("D", 0.4)]
Wie kann ich das machen?
Code: Alles auswählen
seq = [("A", 0.25), ("B", 0.25), ("C", 0.2), ("D", 0.4)]
"Tulpe" ist gut!microkernel hat geschrieben:Die Kommazahl in jeder Tulpe gibt die Wahrscheinlichkeit an, ob ein Buchstabe gewählt wird.
Code: Alles auswählen
from __future__ import print_function
from __future__ import unicode_literals
from bisect import bisect
from collections import defaultdict
import random
seq = [("A", 0.25), ("B", 0.25), ("C", 0.2), ("D", 0.4)]
last = 0
breakpoints = []
for value in seq:
breakpoints.append(last + value[1])
last += value[1]
print(breakpoints)
Code: Alles auswählen
d = defaultdict(int)
for count in xrange(11000):
pos = bisect(breakpoints, random.random() * last)
d[seq[pos]] += 1
print(d)
Code: Alles auswählen
import random
def weighted_choice(items):
"""items is a list of tuples in the form (item, weight)"""
assert map(lambda lst: float(lst[1]), items).count(1.0) <= 1
weight_total = sum((item[1] for item in items))
n = random.uniform(0, weight_total)
for item, weight in items:
if n < weight or weight == 1.0:
return item
n -= weight
return item