13.4 최다 빈출 단어
최다 빈출 단어를 찾기 위해 각 튜플에는 단어와 빈도를 저장한 튜플의 리스트를 만들고, 이를 정렬할 것이다.
다음 함수는 히스토그램을 받아서 단어-빈도 튜플의 리스트를 반환한다.
def most_common(hist):
t = []
for key, value in hist.items():
t.append((value, key))
t.sort(reverse=True)
return t
각 튜플에는 빈도를 먼저 표시하므로 결과 목록도 빈도로 정렬된다. 다음 루프는 최다 빈출 단어 10개를 출력한다.
t = most_common(hist)
print('The most common words are:')
for freq, word in t[:10]:
print(word, freq, sep='\t')