Clustering the keys of a dict according to its values

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Nov 14 08:24:49 EST 2008


Alternative version:

def cluster(data):
    d = defaultdict(list)
    pairs = enumerate(data) if isinstance(data, list) else
data.iteritems()
    for k, v in pairs:
        d[v].append(k)
    return d

Bye,
bearophile



More information about the Python-list mailing list