code: def weighted_sampler(seq, weights):
      "Return a random-sample function that picks from seq weighted by weights."
      totals = []
      for w in weights:
          totals.append(w + totals[-1] if totals else w)
      return lambda: seq[bisect.bisect(totals, random.uniform(0, totals[-1]))f weighted_sampler(seq, weights):
      "Return a random-sample function that picks from seq weighted by weights."
      totals = []
      for w in weights:
          totals.append(w + totals[-1] if totals else w)
      return lambda: seq[bisect.bisect(totals, random.uniform(0, totals[-1]))]
docs: Return a random-sample function that picks from seq weighted by weights.