[파이썬] Most Common Words
인자로 받은 txt 파일에서 가장 자주 나온 단어 순으로 출력하는 프로그램
ximport sysfrom collections import Counter
try: num_words = int(sys.argv[1]) #두번째 인자except: print("usage: most_common_words.py nuim_words") sys.exit()
counter = Counter(word.lower() for line in sys.stdin for word in line.strip().split() if word)for word, count in counter.most_common(num_words): sys.stdout.write(str(count)) sys.stdout.write("\t") sys.stdout.write(word) sys.stdout.write("\n")xxxxxxxxxx$ the_bible.txt | python3 most_common_words.py 10결과:
xxxxxxxxxx64193 the51380 and34753 of13643 to12799 that12560 in10263 he9840 shall8987 unto8836 for
Counter 사용이 핵심이다
'Data_Python' 카테고리의 다른 글
| [Numpy] ddof 란? Degrees of Freedom 자유도 (np.var / np.std) (0) | 2022.04.16 |
|---|