>>> words=input('enter your sensence:')
enter your sensence:it was the best of times it was the worst of times it was the age of wisdom it was the age of foolishnes
>>> wordcount={}
>>> for word in words.split():
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
>>> print(word, wordcount)
foolishnes {'age': 2, 'of': 4, 'it': 4, 'wisdom': 1, 'was': 4, 'the': 4, 'worst': 1, 'times': 2, 'foolishnes': 1, 'best': 1}
答案 0 :(得分:0)
你已经有了自己的文字数量。以下是按排序顺序打印出来的方法:
def printout(wordcount):
for k in sorted(wordcount, key=lambda k: (wordcount[k], k)):
print(k, wordcount[k])