[Tutor] creat a program that reads frequency of words in file

Alan Gauld alan.gauld at btinternet.com
Tue Jun 2 09:19:52 CEST 2015


On 02/06/15 00:42, Alan Gauld wrote:

>>              forwordintext:
>>                  ifwordnot inwords:
>>                      words[word] =1
>>                  else:
>>                      words[word] +=1
>
> Look into the setdefault() method of dictionaries.
> It can replace the if/else above.
>

On reflection, the get() method might be even more useful.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list