コンテンツにスキップ

defaultdict

1. overview

  • 辞書型の型を指定することが出来る

2. 引数

説明
int defaultdict(int)
str defaultdict(str)
bool defaultdict(bool)
list defaultdict(list)
set defaultdict(set)

3. basic

3.1. NG例

python ソースコード
hashmap = {}

hashmap["a"] = 1
hashmap["a"] += 1
hashmap["b"] += 1 
python 出力結果
  File "/Users/kaz/Work/python-sandbox/sample.py", line 418, in <module>
    hashmap["b"] += 1
KeyError: 'b'

3.2. OK例(defaultdict)

python ソースコード
from collections import defaultdict

hashmap = defaultdict(int)
hashmap["a"] = 1
hashmap["a"] += 1
hashmap["b"] += 1 defaultdictでint型を指定しているためエラーにならない
print(hashmap)
python 出力結果
defaultdict(<class 'int'>, {'a': 2, 'b': 1})

4. reference