defaultdict

defaultdict Python의 collections 모듈에 defaultdict이 있다. 코딩테스트 풀때, 딕셔너리가 필요한 문제 풀 때 굉장히 편리하다 일반 딕셔너리는 key 값을 미리 넣어주지 않았다면 value에 대한 연산을 할 때 KeyError 에러가 난다. words = ["apple", "apple", "iphone", "mac", "mac", "mac"] dict_temp = {} dict_temp[words[0]] += 1 그렇다보니 defaultdict을 모르기 이전에는 이런식으로 코드를 짠 것 같다 for word in words: if word in dict_temp.keys(): dict_temp[word] += 1 else: dict_temp[word] = 1 이런 번거로움..
minjiwoo
'defaultdict' 태그의 글 목록