[Python] Counter()로 list 요소의 중복 갯수 출력하기

도갱도갱·2021년 12월 1일
0

Python

목록 보기
6/34
post-thumbnail

collections의 Counter 클래스로 리스트 내의 중복된 요소의 갯수를 출력하고 이를 딕셔너리 자료형으로 변환할 수 있다.

from collections import Counter

counter = Counter(['a', 'b', 'a', 'c', 'b', 'b', 'b'])
print(counter['a'])
print(counter['b'])
print(counter['c'])

print(dict(counter))

결과

0개의 댓글