def pick(elem, counters): weights = [(elem in v) * 0.5 + 0.5 for v in counters.values()] return random.choices(list(counters), weights)[0]