1
2
3
4
5
6
7
8
9
10
11
12
13
| article = list(open("The_Red_Headed_League.txt", "r", encoding="utf-8"))
books = {}
for line in article:
words = line.replace(",", " ").replace(".", " ").split()
for word in words:
lower_word = word.lower()
if lower_word not in books:
books[lower_word] = 0
books[lower_word] += 1
word_list = sorted(books.items(), key=lambda x: (-x[1], x[0]))
writer = open("word_list.txt", "w")
for word, count in word_list:
writer.write(f"{word} {count}\n")
|