All Samples(1) | Call(0) | Derive(0) | Import(1)

Shuffles the sequence x in place. Elements that are "heavier" will be more likely to be at the beginning. The `key_weight` argument can be used to define how to access the element's weight. By default, we assume x to be a sequence of (element, weight) tuples.

def weighted_shuffle(x, key_weight=None): """Shuffles the sequence x in place. Elements that are "heavier" will be more likely to be at the beginning. The `key_weight` argument can be used to define how to access the element's weight. By default, we assume x to be a sequence of (element, weight) tuples. """ if key_weight is None: key_weight = lambda t: t[1] weights = [(i, key_weight(e)) for e in enumerate(x)] for i in reversed(xrange(1, len(x))): j = weighted_pick(weights[:i+1]) x[i], x[j] = x[j], x[i]

**django-carousel**(Download)

from django.utils.translation import ugettext_lazy as _ from utils import weighted_shuffle, shuffled class Carousel(models.Model):