Issue in printing top 20 dictionary items by dictionary value

MRAB python at mrabarnett.plus.com
Sat Oct 4 15:40:18 EDT 2014


On 2014-10-04 18:36, Shiva wrote:
 > It works :
 > orderedwords = sorted(textstorage.keys(), key=textstorage.get)
 >
 > The method textstorage.get will accept a word and return it's value
 > which in this instance is the count.
 >
 > What I don't understand is:
 >
 >      for w in eachword:
 >          textstorage[w]=textstorage.get(w, 0) + 1
 >
 > How does textstorage.get(w,0)+1 give the count of the word??
 >
What textstorage.get(w, 0) says is: if the word is in the dict, return
its count, else return 0.




More information about the Python-list mailing list