coding test/etc
[Python] defaultdict 라이브러리
코테 준비하면서 다른 사람들의 분들의 풀이를 보면 `dict` 대신 `defaultdict`를 사용하는 경우를 종종 보았다. 그래서 `defaultdict` 라이브러리에 대해 알아보고자 정리하였다.defualtdict 라이브러리란?`collections` 모듈에서 제공되는 딕셔너리(dict)의 서브 클래스존재하지 않는 키에 접근했을 때, 미리 지정한 기본 값으로 키값 초기화키를 추가하려 할 때 키가 존재하지 않아 발생하는 `KeyError` 방지할 수 있음 일반 `dict`의 경우 d = {}d["a"] += 1 # KeyError`defualtdict`의 경우from collections import defaultdictd = defaultdict(int)d["a"] += 1print(d["a"]) ..