I have a dictionary where each key has a list of variable length, eg:
d = { \'a\': [1, 3, 2], \'b\': [6], \'c\': [0, 0] }
Is there a cle
Without constructing a new, possibly big list with repeated values:
def select_weighted(d): offset = random.randint(0, sum(d.itervalues())-1) for k, v in d.iteritems(): if offset < v: return k offset -= v