Skip to content

Most common word · ⁨出现最多的单词⁩

English

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 计数。选择:按原来的顺序遍历单词,只有次数严格更大时才换成这个单词,这样次数相同时就会留下先出现的单词。

Log in or create account · ⁨登录或创建账户⁩

IGCSE, A-Level · ⁨IGCSE、A-Level⁩ & AP · ⁨与 AP⁩