Created
March 23, 2022 14:02
-
-
Save yogeshvar/ca5f2d815f657265675af84f7405411a to your computer and use it in GitHub Desktop.
WordCount From Given File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import Counter | |
import re | |
with open('test.txt') as f_input: | |
text = f_input.read().lower() | |
words = re.findall(r'\b(\w+)\b', text) | |
word_counts = Counter(w for w in words if len(w) == 20) | |
sumCount = 0 | |
for word, count in word_counts.items(): | |
sumCount += count | |
print('Total:', sumCount) | |
with open('test.txt') as f_input: | |
text = f_input.read().lower() | |
words = re.findall(r'\b(\w+)\b', text) | |
word_counts = Counter(w for w in words if len(w) == 20) | |
sumCount = 0 | |
unique_words = set() | |
for word, count in word_counts.items(): | |
unique_words.add(word) | |
print(word, count) | |
print('Unique:', len(unique_words)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment