Python统计单词出现次数
因为作者能力有限,不会单词出现的频次进行统计,所以各位将着看吧!
sentence = 'beautiful is batter than ugly.Explicit is batter than impanct beautiful,is and,Simpleis batter'
for ch in ", . ? ! ":
sentence = sentence.replace(ch, " ")
words = sentence.split()
map1 = {}
for word in words:
if word in map1:
map1[word] += 1
else:
map1[word] = 1
items = list(map1.items())
items.sort()
#print(items)
for item in items:
word, count = item
print("{:<12}{:>5}".format(word, count))