Clustering the keys of a dict according to its values

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Nov 14 10:57:21 EST 2008


bearophileHUGS at lycos.com a écrit :
> Alternative version:
> 
> def cluster(data):
>     d = defaultdict(list)
>     pairs = enumerate(data) if isinstance(data, list) else
> data.iteritems()


What is data is another type of sequence or iterable ?-)



>     for k, v in pairs:
>         d[v].append(k)
>     return d
> 
> Bye,
> bearophile



More information about the Python-list mailing list