get method
Aaron Brady
castironpi at gmail.com
Mon Dec 29 21:35:21 EST 2008
On Dec 29, 8:02 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> On Mon, 29 Dec 2008 17:38:36 -0800, Ross wrote:
> > On Dec 29, 8:07 pm, Scott David Daniels <Scott.Dani... at Acm.Org> wrote:
> >> Ross wrote:
> >> > ... Use get to write histogram more concisely. You should be able to
> >> > eliminate the if statement.
>
> >> > def histogram(s):
> >> > d = dict()
> >> > for c in s:
> >> > d[c]= d.get(c,0)
> >> > return d
>
> >> > This code returns a dictionary of all the letters to any string s I
> >> > give it but each corresponding value is incorrectly the default of 0.
> >> > What am I doing wrong?
>
> >> How is this code supposed to count?
>
> >> --Scott David Daniels
> >> Scott.Dani... at Acm.Org
>
> > I realize the code isn't counting, but how am I to do this without using
> > an if statement as the problem instructs?
snip
> So what you need is:
>
> * set d[c] to whatever d[c] already is plus one, or 0 plus one if it
> isn't already there.
>
> It's a two character change to one line. Let us know if you still can't
> see it.
That's actually really sophisticated. Steven is being really clever
here, by writing a number in an unusual way.
He said:
* set d[c] to whatever d[c] already is plus one, or 0 plus one if it
isn't already there.
It is the same as:
* set d[c] to whatever d[c] already is plus one, or one if it
isn't already there.
Side-by-side. Perhaps you will learn his secret someday. It has to
do with 0+1=1.
Armed with it, he has:
something something +1, something something something +1
Which brings us (to):
( something something, something something something ) +1
Three cheers for thinking.
More information about the Python-list
mailing list