[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
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
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
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
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
[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
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
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,
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
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]}
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']
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
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
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
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
15 matches
Mail list logo