ID_DI
DI's study notes
ID_DI
전체 방문자
오늘
어제
  • 분류 전체보기 (85)
    • Deep-Learning (3)
      • CNN (2)
      • NLP (1)
    • Data_Python (6)
      • Numpy (0)
      • Matplotlib (4)
    • Python (8)
      • Python Algorithm (6)
    • Java (36)
      • Java(base) (33)
      • Java practice(base) (2)
    • Git (12)
    • Algorithm (7)
    • etc (7)
    • linux (1)
    • DeskSetup (0)
    • TIL_모각코 (4)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • string to int
  • staged
  • 정렬
  • java 기초
  • 자바
  • git
  • 파이썬
  • java.lang
  • java
  • binarySearch
  • Github
  • java.net
  • 알고리즘
  • git add.
  • README.md
  • 커밋
  • java base
  • matplotlib
  • Python
  • 합병정렬

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
ID_DI

DI's study notes

[파이썬] Most Common Words 가장 자주 사용된 단어 추출
Data_Python

[파이썬] Most Common Words 가장 자주 사용된 단어 추출

2022. 5. 25. 00:55
MostCommonWords

[파이썬] Most Common Words

인자로 받은 txt 파일에서 가장 자주 나온 단어 순으로 출력하는 프로그램

​x
import sys
from 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

결과:

xxxxxxxxxx
64193   the
51380   and
34753   of
13643   to
12799   that
12560   in
10263   he
9840    shall
8987    unto
8836    for

Counter 사용이 핵심이다

'Data_Python' 카테고리의 다른 글

[Numpy] ddof 란? Degrees of Freedom 자유도 (np.var / np.std)  (0) 2022.04.16
    'Data_Python' 카테고리의 다른 글
    • [Numpy] ddof 란? Degrees of Freedom 자유도 (np.var / np.std)
    ID_DI
    ID_DI
    Computer Vision

    티스토리툴바