Skip to content

Instantly share code, notes, and snippets.

@yogeshvar
Created March 23, 2022 14:02
Show Gist options
  • Save yogeshvar/ca5f2d815f657265675af84f7405411a to your computer and use it in GitHub Desktop.
Save yogeshvar/ca5f2d815f657265675af84f7405411a to your computer and use it in GitHub Desktop.
WordCount From Given File
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