Untitled

mail@pastecode.io avatar
unknown
python
a year ago
465 B
10
Indexable
Never
filename = input('What file to count words? ') # 1
wordcounts = {} # 2a
with open(filename) as infile: # 2
    for line in infile: # 3
        for word in line.lower().split(): # 4
            if word in wordcounts: # 5
                wordcounts[word] += 1 # 6
            else: # 7
                wordcounts[word] = 1 # 7a
                
for word in sorted(wordcounts, key=wordcounts.get, reverse=True): # 8, sorted by key
    print(word, wordcounts[word]) # 9