Pythonのdictは存在しないキーにアクセスするとエラーになるので、defaultdictを使おう
from collections import defaultdict d = defaultdict(int)
Register as a new user and use Qiita more conveniently