Most common word · Mot le plus fréquent
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.
Mot le plus fréquent
Trouver le mot favori d'un texte.
Travailler en trois étapes. Nettoyer : supprimer la ponctuation avec .replace() et tout mettre en minuscules avec .lower(). Compter : .split() le texte en mots et les compter dans un dictionnaire avec counts.get(word, 0) + 1. Choisir : parcourir les mots dans leur ordre d'origine et passer à un mot seulement si son comptage est strictement supérieur, de sorte qu'en cas d'égalité, le mot le plus ancien l'emporte.
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. · Écrivez most_common_word(text) qui retourne le mot utilisé le plus souvent, en ignorant la casse et les signes de ponctuation .,!?. Si deux mots sont à égalité, retournez celui qui apparaît en premier.
Click Run to see the output here. · Cliquez sur Exécuter pour voir le résultat ici.