Re: [Tutor] setdefault method

2006-03-28 Thread Carroll, Barry
Terry and Kent: Thanks for your timely replies. I agree: its creator could have chosen a more intuitive name for setdefault. Regards, Barry [EMAIL PROTECTED] 541-302-1107 We who cut mere stones must always be envisioning cathedrals. -Quarry worker's creed _

Re: [Tutor] setdefault method

2006-03-27 Thread Kent Johnson
Terry Carroll wrote: freq = {} sampletext = "norwegian blue" for char in sampletext: > > ... freq[char] = freq.setdefault(char,0)+1 Although I'm a big fan of setdefault() I think this particular example is better written as freq[char] = freq.get(char,0)+1 There is no need to s

Re: [Tutor] setdefault method

2006-03-27 Thread Kent Johnson
Carroll, Barry wrote: > Greetings: > > What is the purpose of the dictionary method setdefault(k[, x])? setdefault() is perhaps badly named, but it is very useful. It doesn't do what you think it does! From the docs: setdefault() is like get(), except that if k is missing, x is both return

Re: [Tutor] setdefault method

2006-03-27 Thread Terry Carroll
On Mon, 27 Mar 2006, Carroll, Barry wrote: > So, what then is the proper use of setdefault()? And, if d.setdefault > does not actually assign a default value for d, is there a way to do > this? It's a somewhat misleading name, but setdefault a) looks up an entry in a dictionary and c) returns i

[Tutor] setdefault method

2006-03-27 Thread Carroll, Barry
Greetings: What is the purpose of the dictionary method setdefault(k[, x])? For example assume the following dictionary: >>> colors = {'green':(0,255,0), 'red':(255,0,0), 'blue':(0,0,255), >>> 'white':(255,255,255), 'black':(0,0,0)} >>> Now, execute the statement: >>> colors.setdefault('black