通过python对字符串中的所有单词进行统计,统计出每个单词出现的次数,并显示出现次数最多的单词
text = "ga bu zo meuh ga zo bu meuh meuh ga zo zo meuh zo bu zo"
items = text.split(' ')
counters = {}
for item in items:
if item in counters:
counters[item] += 1
else:
counters[item] = 1
print "Count of different word:"
print counters
print "Most popular word:"
print sorted([(counter,word) for word,counter in counters.items()],reverse=True)[0][1]
#显示结果:
Count of different word:
{'bu': 3, 'zo': 6, 'meuh': 4, 'ga': 3}
Most popular word:
zo
