from collections import defaultdict
from itertools import repeat
import re
input_data = '''
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.'''
word_freq = defaultdict(repeat(0).__next__)
for word in re.findall('\\w+', input_data):
word_freq[word.lower()] += 1
freq_words = defaultdict(list)
for word, freq in word_freq.items():
freq_words[freq].append(word)
print(freq_words)
Item 34: defaultdict
Posted by Logan Chien