728x90
데이터의 개수를 셀 때 유용한 파이썬의 collections 모듈의 Counter 클래스 사용법을 알아보겠습니다.
알파벳 사용빈도 확인
from collections import Counter
Counter('hello world')
실행 결과
Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})
다음으로는 most_common() 함수에 대해 설명하겠습니다.
most_common()함수는 Counter()에서 가장 빈도수가 높은 순으로 표시 해 주는 함수입니다.
인자값으로 숫자를 입력하면 숫자번째까지의 빈도수를 표시합니다.
from collections import Counter
data="hello world"
result=Counter(data)
print(result.most_common())
print(result.most_commom(2))
실행 결과
[('l',3),('o',2),('h',1),('e',1),(' ',1),('w',1),('r',1),('d',1)]
[('l',3),('o',2)]
728x90
반응형
'개발 > Python' 카테고리의 다른 글
[Pyhton/파이썬] 순열과 조합 (0) | 2022.05.14 |
---|---|
[Python] 데크(deque) (0) | 2022.05.13 |
[파이썬] find, index 함수 (0) | 2022.01.13 |
[Python] sort(), sorted(), lambda (0) | 2022.01.12 |
[파이썬] 올림, 내림, 반올림 (0) | 2021.12.27 |