Clustering the keys of a dict according to its values

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Nov 14 16:54:45 CET 2008


Florian Brucker a écrit :
> Hi everybody!
> 
> Given a dictionary, I want to create a clustered version of it, 
> collecting keys that have the same value:
> 
>  >>> d = {'a':1, 'b':2, 'c':1, 'd':1, 'e':2, 'f':3}
>  >>> cluster(d)
> {1:['a', 'c', 'd'], 2:['b', 'e'], 3:['f']}
> 
> That is, generate a new dict which holds for each value of the old dict 
> a list of the keys of the old dict that have that very value.
 >
> Another requirement is that it should also work on lists, in that case 
> with indices instead of keys. We may assume that all values in the 
> original dict/list can be used as dict keys.

from collections import defaultdict

def cluster(source):
     iteritems = getattr(
         source, "iteritems",
         lambda : enumerate(source)
         )
     index = defaultdict(list)
     for k, v in iteritems():
          index[k].append(v)
     return index

HTH



More information about the Python-list mailing list