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 PROTECTED] wrote:
 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 bound to the second
 argument, and then that second argument is returned as the value.
 
 (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.)
 
 -Peter
 --
 http://mail.python.org/mailman/listinfo/python-list
 

-- 
http://mail.python.org/mailman/listinfo/python-list


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 if the key was missing.

James, I'll assume your reply was intended to address my comment above.

It's not so much that the concept of set the default value for this 
key is poorly captured by the name setdefault, but that the function 
is used almost exclusively in the idiom below, where it is critical that 
it also _returns_ the value, which is usually then operated on 
immediately, usually in the same line of code.

dict.setdefault(key, defaultValue).someMethodOnKey()

or

dict.setdefault(key, defaultValue) #= value, where # is some operator.

I suppose I shouldn't blame setdefault() itself for being poorly named, 
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 more important than the fact that a default is set!

I can't think of a better name, though, although I might find foo less 
confusing in the above context. :-)

-Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


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 remind myself to change the reply-to
address to the list.  (other lists do this by default, why not this
one?)

I agree that the name doesn't ring quite right to me either.

I kind of understand what the creator of the function was getting
at... it's kind of like when you want to retrieve a configuration
variable from a container, but if it's not there, then you want to use
a default:

storedWidth = container.GetConfigurationValue(name = width, default=500)

If there is a width stored, it will retrieve that, otherwise it will
give you the default that you specified in the second parameter.  It
makes it easier than checking for existance,  then retrieving or
assigning a default.

Setdefault, in my mind is really a _get_ kind of operation.  It
retrieves from the dictionary most of the time, and only does anything
different when the key doesn't exist, and then does an assignment...

so my next guess at a better name for setdefault would be:

value = container.GetOrAddDefault(key=a, default=[])
value.append(listvalue)

but that's kind of confusing too, but it better describes what is happening.

-Jim

On 7/12/05, Peter Hansen [EMAIL PROTECTED] wrote:
 (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 if the key was missing.
 
 James, I'll assume your reply was intended to address my comment above.

-- 
http://mail.python.org/mailman/listinfo/python-list


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 wink.

 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 more important than the fact that a default is set!

 I can't think of a better name, though, although I might find foo less
 confusing in the above context. :-)

I wanted to call it getorset() -- so much so that even now I sometimes
still type that instead!  The get part reminds me that it's fetching
a value, same as dict.get(key, default) -- or set'ing it too if
there's not already a value to get.  If you have a fancy enough
editor, you can teach it to replace setdefault by getorset whenever
you type the former ;-)
-- 
http://mail.python.org/mailman/listinfo/python-list


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 wink.

 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 more important than the fact that a default is set!

 I can't think of a better name, though, although I might find foo less
 confusing in the above context. :-)

I wanted to call it getorset() -- so much so that even now I sometimes
still type that instead!  The get part reminds me that it's fetching
a value, same as dict.get(key, default) -- or set'ing it too if
there's not already a value to get.  If you have a fancy enough
editor, you can teach it to replace setdefault by getorset whenever
you type the former ;-)

But it isn't get OR set, it's
set_default_if_no_value_then_either_way_effectively_get ;-)
Regards,
Bengt Richter
-- 
http://mail.python.org/mailman/listinfo/python-list


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?

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.

NewDict = {'This is repeated':['rt','sr'],'This is not':['gf']}

I am still learning Python and have struggled with this for hours before 
deciding to go for help.  Unfortunately, I didn't really know how to search 
for this in google and decided to post it here.  I apologise if this is too 
basic for this newsgroup...

Ric 


-- 
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 300 items, some of the values beingrepeated.Both keys and values are strings.How can I turn this thing onits 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?It is hard to explain but this is what I mean:Dict = {'rt': 'This is repeated', 'sr': 'This is repeated', 'gf': 'This isnot'}I want this to return a new dict with string keys and lists containing the
previous keys for repeated values.NewDict = {'This is repeated':['rt','sr'],'This is not':['gf']}I am still learning Python and have struggled with this for hours beforedeciding to go for help.Unfortunately, I didn't really know how to search
for this in google and decided to post it here.I apologise if this is toobasic for this newsgroup...Ric--http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list

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
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.setdefault(v, []).append(k)Regards, mawe--
http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list

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.
 
 NewDict = {'This is repeated':['rt','sr'],'This is not':['gf']}

NewDict = {}
for x in Dict.keys():
try:
NewDict[Dict[x]].append(x)
except KeyError:
NewDict[Dict[x]] = [x]

-- 
Mark Jackson - http://www.alumni.caltech.edu/~mjackson
It is difficult for men in high office to avoid
the malady of self-delusion.- Calvin Coolidge


-- 
http://mail.python.org/mailman/listinfo/python-list


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 
 previous keys for repeated values.
 
 NewDict = {'This is repeated':['rt','sr'],'This is not':['gf']}
 
 NewDict = {}
 for x in Dict.keys():
   try:
   NewDict[Dict[x]].append(x)
   except KeyError:
   NewDict[Dict[x]] = [x]

Or, more up-to-date:

NewDict = {}
for key, val in Dict.iteritems():
NewDict.setdefault(val, []).append(key)

Reinhold
-- 
http://mail.python.org/mailman/listinfo/python-list


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 Birkenfeld [EMAIL PROTECTED] wrote in 
message news:[EMAIL PROTECTED]
 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
 previous keys for repeated values.

 NewDict = {'This is repeated':['rt','sr'],'This is not':['gf']}

 NewDict = {}
 for x in Dict.keys():
 try:
 NewDict[Dict[x]].append(x)
 except KeyError:
 NewDict[Dict[x]] = [x]

 Or, more up-to-date:

 NewDict = {}
 for key, val in Dict.iteritems():
NewDict.setdefault(val, []).append(key)

 Reinhold 


-- 
http://mail.python.org/mailman/listinfo/python-list


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!  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 Birkenfeld [EMAIL PROTECTED] wrote in 
 message news:[EMAIL PROTECTED]
 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
 previous keys for repeated values.

 NewDict = {'This is repeated':['rt','sr'],'This is not':['gf']}

 NewDict = {}
 for x in Dict.keys():
 try:
 NewDict[Dict[x]].append(x)
 except KeyError:
 NewDict[Dict[x]] = [x]

 Or, more up-to-date:

 NewDict = {}
 for key, val in Dict.iteritems():
NewDict.setdefault(val, []).append(key)

 Reinhold

 


-- 
http://mail.python.org/mailman/listinfo/python-list


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 bound to the second 
argument, and then that second argument is returned as the value.

(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.)

-Peter
-- 
http://mail.python.org/mailman/listinfo/python-list