get method

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Mon Dec 29 20:06:26 EST 2008


On Mon, 29 Dec 2008 17:00:31 -0800, Ross wrote:

> Here's my code:
> 
> 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?

You're forgetting to increase the count each time you see a letter:

* Look up the letter c in the dict, and call it count;
* If c isn't found in the dict, use 0 as the count.
* Set the value to count.

But at no point do you increase count.


-- 
Steven



More information about the Python-list mailing list