使用Python 3.2版本。
import collections
d = defaultdict(int)
运行
NameError: name 'defaultdict' is not defined
我已经重新启动了Idle。我知道正在导入collections,因为输入
collections
导致结果
<module 'collections' from '/usr/lib/python3.2/collections.py'>
同时help(collections)向我展示了包括defaultdict类的帮助信息。
我做错了什么?
from collections import defaultdict
,或者您可以将defaultdict
称为collections.defaultdict
。 - Kyle Strand