Clustering the keys of a dict according to its values

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Nov 14 14:24:49 CET 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