Dev/Python

[Python] counter

yujin.me 2021. 6. 15. 19:08

counter

  • collections 모듈의 Counter 클래스
  • collections.Counter를 사용
  • 데이터를 개수를 셀 때 유용
  • 오름차순으로 정렬
import collections

list_tmp = collections.Counter('hello world')
print(list_tmp)
# Counter({'l': 3, 'o': 2, 'h': 1, 'e': 1, ' ': 1, 'w': 1, 'r': 1, 'd': 1})

max_val = max(list_tmp.values())
print(max_val)
# 3

 

반응형