ღ yuni_world ღ

[Python] collections 모듈 Counter 본문

Python

[Python] collections 모듈 Counter

ღ유닝이ღ 2023. 3. 15. 19:32

파이썬에서 collections 모듈은 별도의 패키지 설치 없이 import해서 사용이 가능하다.

임포트해서 사용하는 방법은 아래와 같이 두가지가 있다.

import collections
collections.Counter(a)

from collections import Counter
Counter(a)

Counter은 iterable한 객체(리스트나 문자열 등)를 같은것끼리 묶고, 그 개수를 value값으로 받아 dict형태로 나타낸다.

import collections

a=['a','b','c','a']
collections.Counter(a)

>>> Counter({'a': 2, 'b': 1, 'c': 1})