Most common word · 出现最多的单词
Most common word
Find the favourite word of a piece of text.
Work in three stages. Clean: remove the punctuation with .replace() and make everything lowercase with .lower(). Count: .split() the text into words and count them in a dictionary with counts.get(word, 0) + 1. Choose: walk through the words in their original order and switch to a word only when its count is strictly bigger, so that a tie goes to the earlier word.
出现最多的单词
找出一段文字里最常用的单词。
分三步来做。清理:用 .replace() 去掉标点,用 .lower() 全部变成小写。计数:用 .split() 把文字分成单词,再用字典和 counts.get(word, 0) + 1 计数。选择:按原来的顺序遍历单词,只有次数严格更大时才换成这个单词,这样次数相同时就会留下先出现的单词。
Write most_common_word(text) that returns the word used most often, ignoring upper and lower case and the punctuation marks .,!?. If two words tie, return the one that appears first. · 编写 most_common_word(text),返回出现次数最多的单词,忽略大小写和标点 .,!?。如果有两个单词次数相同,返回先出现的那个。
Click Run to see the output here. · 点击“运行”查看此处输出。