본문 바로가기
반치용/기타 및 저장

[파이썬]글 내의 단어 빈도 체크

by  반  2020. 6. 17.

 

count = {} # 딕셔너리 생성
for j in open('jupytorNotebooks/poet.txt').read().replace("\n","").replace(".","").split() : count[j] = count.get(j,0) + 1 # items 생성
[(x,y) for (y,x) in sorted([(x, y) for (y, x) in count.items()], reverse=True)[:11]] # 정렬

쓸 데 없는 집착이긴 한데, 더 줄일 방법이 있을까?

 

count = {} # 딕셔너리 생성
for j in "apple apple monkey snail apple monkey chiken".replace("\n","").replace(".","").split() : count[j] = count.get(j,0) + 1 # items 생성
[(x,y) for (y,x) in sorted([(x, y) for (y, x) in count.items()], reverse=True)[:11]] # 정렬

이걸로 테스트 ㄱㄱ

속도도 나쁘지 않은 듯

댓글