[Python] python统计字符串中不同单词的数量 →→→→→进入此内容的聊天室

来自 , 2019-02-09, 写在 Python, 查看 170 次.
URL http://www.code666.cn/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

回复 "python统计字符串中不同单词的数量"

这儿你可以回复上面这条便签

captcha