คำที่พบบ่อยที่สุด
หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
อันนี้สร้างจากบทเรียนที่คุณยังไม่เสร็จสิ้น ให้ทำบทเรียนนั้นก่อน:
Python Basics · Dictionaries (records) →
คำที่พบบ่อยที่สุด
หาคำโปรดของข้อความหนึ่งๆ
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.
ความยาก 5/5 · 400 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
เขียน most_common_word(text) ที่กลับคืนคำที่ใช้บ่อยที่สุด โดยไม่สนใจตัวพิมพ์ใหญ่และพิมพ์เล็ก และเครื่องหมายวรรคตอน .,!?. หากมีคำที่ติดกัน ให้กลับคืนคำที่ปรากฏก่อน
คลิก Run เพื่อดูผลลัพธ์ที่นี่