Python counts the number of different words in a string

From , 5 Years ago, written in Python, viewed 206 times.
URL https://pastebin.vip/view/98c56bce
  1. text = "ga bu zo meuh ga zo bu meuh meuh ga zo zo meuh zo bu zo"
  2. items = text.split(' ')
  3.  
  4. counters = {}
  5. for item in items:
  6.     if item in counters:
  7.         counters[item] += 1
  8.     else:
  9.         counters[item] = 1
  10.  
  11. print "Count of different word:"
  12. print counters
  13.  
  14. print "Most popular word:"
  15. print sorted([(counter,word) for word,counter in counters.items()],reverse=True)[0][1]
  16.  
  17. #显示结果:
  18.  
  19. Count of different word:
  20. {'bu': 3, 'zo': 6, 'meuh': 4, 'ga': 3}
  21. Most popular word:
  22. zo
  23. #//python/1871

Reply to "Python counts the number of different words in a string"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website