用python代码定义变量saying = ['After', 'all', 'is', 'said', 'and', 'done', 'more', 'is', 'said', 'than', 'done'] 编写程序统计saying中出现的单词次数(分别按重复出现的单词记多次和记一次统计)。将单词按字典序排序并显示,再显示排在最后的2个单词,最后将根据saying生成字符串,单词之间用空格分隔。并展示代码
时间: 2023-03-15 09:55:26 浏览: 147
Python中is与==判断的区别
from collections import Counter
saying = ['After', 'all', 'is', 'said', 'and', 'done', 'more', 'is', 'said', 'than', 'done']
count = Counter(saying)
words_sorted = sorted(count.keys())
print('单词按字典序排序显示:', words_sorted)
print('排在最后的2个单词:', words_sorted[-2:])
statement = ' '.join(saying)
print('将saying生成字符串:', statement)代码如下:from collections import Counter
saying = ['After', 'all', 'is', 'said', 'and', 'done', 'more', 'is', 'said', 'than', 'done']
count = Counter(saying)
words_sorted = sorted(count.keys())
print('单词按字典序排序显示:', words_sorted)
print('排在最后的2个单词:', words_sorted[-2:])
statement = ' '.join(saying)
print('将saying生成字符串:', statement)
阅读全文