Python में शब्दों की Frequency गिनना
Frequency table बताता है कि हर normalized word कितनी बार आया है।
में शब्दों की Frequency गिनना क्या है?
Frequency table बताता है कि हर normalized word कितनी बार आया है।
collections.Counter से repetitions गिनें।
इसका उपयोग कब करें?
- चलने योग्य code से Python syntax सीखें।
- बड़ी programming समस्याओं से पहले मजबूत आधार बनाएँ।
- स्थानीय setup के बिना किसी विचार को तुरंत जाँचें।
उदाहरण कोड
main.py
from collections import Counter
import re
text = "Python is clear. Python is practical."
words = re.findall(r"[a-z]+", text.lower())
print(Counter(words))
अपेक्षित आउटपुट
Counter({'python': 2, 'is': 2, 'clear': 1, 'practical': 1})
यह कैसे काम करता है
Text को normalize करने के बाद Counter एक pass में सभी words गिनता है।
मान बदलें और Python इंस्टॉल किए बिना CodeUtility ऑनलाइन Python कंपाइलर में प्रोग्राम चलाएँ।
अभ्यास के कार्य
बड़े dataset पर जाने से पहले input बदलें और edge cases की जाँच करें।
- Code चलाने से पहले output का अनुमान लगाएँ।
- खाली और गलत input संभालें।
- Logic को function में रखें और tests जोड़ें।