Re: Tricky Dictionary Question from newbie

2005-07-12 Thread Bengt Richter
On Tue, 12 Jul 2005 11:52:41 -0400, Tim Peters <[EMAIL PROTECTED]> wrote: >[Peter Hansen] >... >> I suppose I shouldn't blame setdefault() itself for being poorly named, > >No, you should blame Guido for that . > >> but it's confusing to me each time I see it in the above, because the >> name does

Re: Tricky Dictionary Question from newbie

2005-07-12 Thread Tim Peters
[Peter Hansen] ... > I suppose I shouldn't blame setdefault() itself for being poorly named, No, you should blame Guido for that . > but it's confusing to me each time I see it in the above, because the > name doesn't emphasize that the value is being returned, and yet that > fact is arguably mor

Re: Tricky Dictionary Question from newbie

2005-07-12 Thread James Carroll
Oops.. Gmail just normally puts the reply to at the bottom of the discussion... so by default I reply to the list, and the last person to post. My comment was not directed at you. I just posted the contents of an interactive session that I did to better understand setdefault myself. I've got to

Re: Tricky Dictionary Question from newbie

2005-07-12 Thread Peter Hansen
(Fixed top-posting) James Carroll wrote: > On 7/11/05, Peter Hansen <[EMAIL PROTECTED]> wrote: >>(I always have to ignore the name to think about how it works, or it >>gets in the way of my understanding it. The name makes fairly little >>sense to me.) > Notice the dictionary is only changed

Re: Tricky Dictionary Question from newbie

2005-07-12 Thread James Carroll
Notice the dictionary is only changed if the key was missing. >>> a = {} >>> a.setdefault("a", "1") '1' >>> a.setdefault("a", "2") '1' >>> a.setdefault("b", "3") '3' >>> a {'a': '1', 'b': '3'} >>> a.setdefault("a", "5") '1' >>> a {'a': '1', 'b': '3'} -Jim On 7/11/05, Peter Hansen <[EMAIL PROTECT

Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Peter Hansen
Ric Da Force wrote: > How does setdefault work exactly? I am looking in the docs and can't figure > it out... If the key (the first argument) already exists in the dictionary, the corresponding value is returned. If the key does not exist in the dictionary, it is stored in the dictionary and b

Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Ric Da Force
How does setdefault work exactly? I am looking in the docs and can't figure it out... Ric "Ric Da Force" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Thank you guys! (Reinhold, Mark and Markus) I must confess that I am > absolutely awe struck at the power of this language! Th

Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Ric Da Force
Thank you guys! (Reinhold, Mark and Markus) I must confess that I am absolutely awe struck at the power of this language! There is no way in the world that I would have envisaged such simple and elegant solutions!!! Reinhold, is your solution specific to 2.4? Kind Regards, Ric "Reinhold Bir

Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Reinhold Birkenfeld
Mark Jackson wrote: > "Ric Da Force" <[EMAIL PROTECTED]> writes: > >> It is hard to explain but this is what I mean: >> >> Dict = {'rt': 'This is repeated', 'sr': 'This is repeated', 'gf': 'This is >> not'} >> >> I want this to return a new dict with string keys and lists containing the >> pre

Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Mark Jackson
"Ric Da Force" <[EMAIL PROTECTED]> writes: > It is hard to explain but this is what I mean: > > Dict = {'rt': 'This is repeated', 'sr': 'This is repeated', 'gf': 'This is > not'} > > I want this to return a new dict with string keys and lists containing the > previous keys for repeated values.

Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Cyril Bazin
Hum... I think an iteritems is better, this way, python don't need to create in memory a complete list of couple key, value.On 7/11/05, Markus Weihs <[EMAIL PROTECTED]> wrote: Hi! Dict = {'rt': 'repeated', 'sr':'repeated', 'gf':'not repeated'} NewDic = {} for k,v in Dict.items(): NewDic.setdef

Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Markus Weihs
Hi! Dict = {'rt': 'repeated', 'sr':'repeated', 'gf':'not repeated'} NewDic = {} for k,v in Dict.items(): NewDic.setdefault(v, []).append(k) Regards, mawe -- http://mail.python.org/mailman/listinfo/python-list

Re: Tricky Dictionary Question from newbie

2005-07-11 Thread Cyril Bazin
Hello, Try that, it may not be the better solution, but it seems to work: #def invertDict(d): #    d2 = {} #    for k, v in d.iteritems(): #    d2.setdefault(v, []).append(k) #    return d2 Cyril On 7/11/05, Ric Da Force <[EMAIL PROTECTED]> wrote: Hi all,I have a dictionary containing about

Tricky Dictionary Question from newbie

2005-07-11 Thread Ric Da Force
Hi all, I have a dictionary containing about 300 items, some of the values being repeated. Both keys and values are strings. How can I turn this thing on its head so that we create a key based on each unique value and build the values based on the keys corresponding to the repeated values? I