First page Back Continue Last page Overview Image

Alternative Lösung mit collection

import re

from collections import Counter

...

def word_freq2(txt):

word_dict = Counter()

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

for word in list_of_words:

word_dict[word] += 1

return word_dict.most_common()

def word_freq3(txt):

return Counter(re.findall(r"\b\w+\b", txt)).most_common()

fobj = open("1984.txt")

text = fobj.read()

fobj.close()

x = word_freq3(text)

print(x[:40])