浙大版《Python程序设计》题目集(第七章)
import math
strs = input()
while True:
if strs[-1] != "#":
strs = strs + "\n" + input()
else:
strs = strs[:-1]
break
strs = strs.lower()
#print(strs)
for ch in '!"#$%&()*+,-.<=>?@[\\]^_`{|}~':
strs = strs.replace(ch," ")
words = strs.split()
#print(words)
counts = {}
for word in words:
if len(word) > 15:
word = word[:15]
counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key = lambda x:x[1],reverse = True)
print(len(items))
for i in range(math.floor(len(items) * 0.1)):
word,count = items[i]
print(count,word)
本文来自博客园,作者:闲晚,转载请注明原文链接:https://www.cnblogs.com/centimeter73/p/15473952.html