- collections 라이브러리의 Counter는 리스트와 같은 객체에서 내부에 원소가 몇 번 등장하는지 알려준다.
from collections import Counter
counter = Counter(['dog', 'cat', 'dog', 'dog', 'cat'])
print(counter['dog'])
print(counter['cat'])
print(dict(counter))
실행결과
3
2
{'dog': 3, 'cat': 2}
'Algorithm > Python' 카테고리의 다른 글
Python - 이진 탐색 구현, 라이브러리 (0) | 2022.02.09 |
---|---|
Python - sorted()에 lambda 이용하여 정렬하기 (0) | 2022.02.04 |
Python - 최대 공약수, 최소 공배수 구하기 (0) | 2022.02.04 |
Python - 중복 순열, 중복 조합 (0) | 2022.02.04 |
Python - 순열과 조합 (0) | 2022.02.04 |