How do I count the number of times each five letter word appears in a text file and then print the five most frequent and least frequent five letter words?
So far this is what I have written going off of some of the answers shown to me so far. I can't get it to give me the words with five letters and print the most frequent and least frequent words.
counter = {}
in_file = open('tale_of_two_cities_ascii.txt', 'r')
content = in_file.read()
for line in in_file:
for word in line.split():
if len(word) != 5: continue
if word not in counter:
counter[word] = 0
counter[word] += 1
words = sorted(counter, key=counter.get)
print("The five most frequent words:", ','.join(words[-5:]))
print("The five least frequent words:", ','.join(words[:5]))