-
Notifications
You must be signed in to change notification settings - Fork 0
/
unique.py
30 lines (24 loc) · 849 Bytes
/
unique.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import re
words = {}
regex = re.compile("[^a-z]")
# read all words from the example.txt text file and count how often they occur
with open("example.txt", "r") as f:
for line in f:
for word in line.split():
# convert word to lowercase as we don't care about case
word = word.lower()
# remove any superfluous comma's and full stops etc.
word = regex.sub("", word)
# add one tot he count if we've seen the word before, else set count to 1
if word in words:
words[word] += 1
else:
words[word] = 1
print("unique words:")
unique_count = 0
# print words that only occur once
for word in words:
if words[word] == 1:
print(">> " + word)
unique_count += 1
print(f"\n>>> {unique_count} unique words seen")