itertools
-
[Python] collections + itertools: 코딩테스트·실무에 유용한 도구 모음업무 자동화/Python 2025. 12. 4. 16:02
1. collections 모듈1-1. Counter – 항목 개수 세기from collections import Counterdata = ["a", "b", "a", "c", "b", "a"]cnt = Counter(data)print(cnt) # Counter({'a': 3, 'b': 2, 'c': 1})print(cnt["a"]) # 3print(cnt.most_common(1)) # [('a', 3)]활용:문자/단어 빈도 분석로그/이벤트 카운팅최빈값 찾기1-2. defaultdict – 기본값이 있는 dictfrom collections import defaultdictscores = [("kim", 10), ("lee", 20), ("kim", 30)]d ..