본문 바로가기
프로그래밍/파이썬(Python)

파이썬(Python) collections Counter

by comflex 2021. 9. 22.
728x90
반응형

collections Counter

리스트의 항목 갯수를 셀수 있습니다.

목차

  • Counter
  • update
  • most_common

Counter

항목과 항목 수를 딕셔너리 형태로 리턴 받을 수 있습니다.

most_common

가장 많은 빈도를 가진 항목 순으로(내림 차순) 정렬된 딕셔너리 데이터를 받을 수 있습니다.

update

기존 항목에 다른 항목을 추가할 수 있습니다.

  • 코드
from collections import Counter

list1  = ['a', 'a', 'b', 'c', 'd', 'c']
list2 = ['b', 'b', 'b']

count = Counter(list1)
print('result #1: ', count)

count.update(list2)
print('result #2: ', count)

print('result #3: ', count.most_common())
  • 출력
result #1:  Counter({'a': 2, 'c': 2, 'b': 1, 'd': 1})
result #2:  Counter({'b': 4, 'a': 2, 'c': 2, 'd': 1})
result #3:  [('b', 4), ('a', 2), ('c', 2), ('d', 1)]
728x90
반응형