본문 바로가기
AI Researcher가 될끄야!/자연어처리

[자연어 처리] 파이썬 기본데이터 구조 및 함수

by 여니여니여 2021. 1. 12.

함수 Collections.Counter

 

분석대상의 빈도를 파악할 수 있음

매개변수를 어떻게 넣냐에 따라 스플릿(단어를 나누는) 기준이 다름.

기본적으로는 하나의 문자열마다 카운팅.

 

 

import collections
a='apple\tapple\tbanana'
collections.Counter(a)

type(collections.Counter(a))

데이터 타입은 collections.Counter

내부적으로는 사전 형식이나 외부적으로는 카운터 데이터 구조.

 

collections.Counter(a).most_common()

카운터 구조를 리스트구조로 바꾸어줌.

 

dict(Counter(a))

이렇게 할 시에는 사전구조로 바뀜.

 

-Counter와 사전 메소드

b=Counter(a)
b.items()
b.keys()
b.values()

사전구조에 해당되는 메소드들 사용가능

 

결과값

dict_items([('a', 5), ('p', 4), ('l', 2), ('e', 2), ('\t', 2), ('b', 1), ('n', 2)])
dict_keys(['a', 'p', 'l', 'e', '\t', 'b', 'n'])
dict_values([5, 4, 2, 2, 2, 1, 2])

 

댓글