Re: Clustering the keys of a dict according to its values

2008-11-16 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit : Bruno Desthuilliers: What is data is another type of sequence or iterable ?-)< The original problem statement was: I did read it, thanks. If the problem changes, then the code has to/can change. When you write code it's better to avoid over-generalization It

Re: Clustering the keys of a dict according to its values

2008-11-16 Thread Gerard flanagan
Florian Brucker wrote: Florian Brucker wrote: 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, gene

Re: Clustering the keys of a dict according to its values

2008-11-15 Thread bearophileHUGS
Bruno Desthuilliers: > What is data is another type of sequence or iterable ?-)< The original problem statement was: Florian Brucker: >Given a dictionary, I want to create a clustered version of it, collecting >keys that have the same value: [...] Another requirement is that it should >also wor

Re: Clustering the keys of a dict according to its values

2008-11-15 Thread Florian Brucker
Wow, thanks everybody! There's a lot to learn for me from these examples... Enjoy your weekend! Florian Florian Brucker wrote: 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

Re: Clustering the keys of a dict according to its values

2008-11-14 Thread Mark Wooding
Florian Brucker <[EMAIL PROTECTED]> wrote: > 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 assum

Re: Clustering the keys of a dict according to its values

2008-11-14 Thread Bruno Desthuilliers
[EMAIL PROTECTED] 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 By

Re: Clustering the keys of a dict according to its values

2008-11-14 Thread Bruno Desthuilliers
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

Re: Clustering the keys of a dict according to its values

2008-11-14 Thread alex23
On Nov 14, 11:24 pm, [EMAIL PROTECTED] 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,

Re: Clustering the keys of a dict according to its values

2008-11-14 Thread bearophileHUGS
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 -- http://mail.python.org/mailman/listinfo/python-list

Re: Clustering the keys of a dict according to its values

2008-11-14 Thread bearophileHUGS
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]}

Re: Clustering the keys of a dict according to its values

2008-11-14 Thread Arnaud Delobelle
On Nov 14, 1:16 pm, Florian Brucker <[EMAIL PROTECTED]> wrote: > 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']

Re: Clustering the keys of a dict according to its values

2008-11-14 Thread alex23
Florian Brucker <[EMAIL PROTECTED]> wrote: > 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

Re: Clustering the keys of a dict according to its values

2008-11-14 Thread Florian Brucker
Are you sure? Is this for school? Yes, I'm pretty sure (the values of the original dict are integers), and no, this isn't for school :) If the "We may assume ..." made you think so, I guess that's the way everybody formulates requirements after having read to many math papers :D If it is of

Re: Clustering the keys of a dict according to its values

2008-11-14 Thread bearophileHUGS
Florian Brucker: > We may assume that all values in the > original dict/list can be used as dict keys. Are you sure? Is this for school? Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Clustering the keys of a dict according to its values

2008-11-14 Thread Florian Brucker
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 o