First page Back Continue Last page Overview Image

Lösung

import re

def word_freq(txt):

word_dict = {}

list_of_words = re.findall(r"\b\w+\b", txt)

for word in list_of_words:

if word in word_dict:

word_dict[word] += 1

else:

word_dict[word] = 1

items = list(word_dict.items())

items.sort(key = lambda x: x[1], reverse=True)

return items

fobj = open("1984.txt")

text = fobj.read()

fobj.close()

y = word_freq(text)

print(x)