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 = dict()
for word in re.findall('\\w+', input_data):
word_freq.setdefault(word.lower(), 0)
word_freq[word.lower()] += 1
freq_words = dict()
for word, freq in word_freq.items():
freq_words.setdefault(freq, []).append(word)
print(freq_words)
Item 33: dict.setdefault() Method
Posted by Logan Chien