Clustering the keys of a dict according to its values

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Fri Nov 14 14:20:29 CET 2008


Not much tested:

from collections import defaultdict

def cluster(pairs):
    """
    >>> d = {'a':1, 'b':2, 'c':1, 'd':1, 'e':2, 'f':3}
    >>> cluster(d) == {1:['a', 'c', 'd'], 2:['b', 'e'], 3:['f']}
    True
    >>> p = [1, 2, 1, 1, 2, 3]
    >>> cluster(p) == {1: [0, 2, 3], 2: [1, 4], 3: [5]}
    True
    """
    d = defaultdict(list)
    if isinstance(pairs, list):
        for k, v in enumerate(pairs):
            d[v].append(k)
    else:
        for k, v in pairs.iteritems():
            d[v].append(k)
    return d

if __name__ == "__main__":
    import doctest
    doctest.testmod()
    print "Doctests finished.\n"

Bye,
bearophile



More information about the Python-list mailing list