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() ข้อความเป็นคำและนับ它们在 dictionary ด้วย 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) ที่คืนคำที่ใช้บ่อยที่สุด โดยovable ตัวพิมพ์ใหญ่-เล็กและเครื่องหมายวรรคตอน .,!?. หากมีคำที่ติดอันดับเท่ากัน ให้คืนคำที่ปรากฏก่อน
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่