3、 实验内容:在英语中英文单词组成句子,在键盘上输入一句含有标点英文语句,统计其中最长的单词以及各单词出现的次数。具体要求如下:
(1) 用户输入一个英文语句,英文语句子中要包含标点符号;若个别单词后面带有标点符号,其标点符号不能计算为单词;
(2) 输出英文语句中最长的单词,若有多个单词,则每行输出一个;
(3) 统计各单词出现的次数,按照数量从多到少排列在屏幕上输出;如果出现的数量相同,则按照单词在句子中的顺序进行显示;输出的格式为:单词左对齐,宽度为10。
例如,如果用户在键盘上输入句子:Hi, we play on the Maldive beach on October the second. 则输出如下图所示:
text = 'Hi, we play on the Maldive beach on October the second.'
text_list = text.replace(',', '').replace('.', '').split(' ')
word_long = {}
stat = {}
for word in text_list:
word_long[word] = len(word)
stat[word] = text_list.count(word)
max_long = max(word_long.values())
for word, long in word_long.items():
if long == max_long:
print(f'maxlen: {max_long} word: {word}')
stat_sorted = sorted(stat.items(), key=lambda x: x[1], reverse=True)
for word in stat_sorted:
print(f'{word[0]:<10}----{word[1]:}')