Clustering the keys of a dict according to its values

alex23 wuwei23 at gmail.com
Fri Nov 14 08:42:38 EST 2008


On Nov 14, 11:24 pm, bearophileH... at lycos.com wrote:
> 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

Very nice, +1.



More information about the Python-list mailing list