Python 未定义 defaultdict
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17767084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
defaultdict is not defined
提问by jason
Using python 3.2.
使用 python 3.2。
import collections
d = defaultdict(int)
run
跑
NameError: name 'defaultdict' is not defined
Ive restarted Idle. I know collections is being imported, because typing
我已经重新启动空闲。我知道正在导入集合,因为输入
collections
results in
结果是
<module 'collections' from '/usr/lib/python3.2/collections.py'>
also help(collections) shows me the help including the defaultdict class.
还 help(collections) 向我展示了包括 defaultdict 类的帮助。
What am I doing wrong?
我究竟做错了什么?
采纳答案by arshajii
>>> import collections
>>> d = collections.defaultdict(int)
>>> d
defaultdict(<type 'int'>, {})
It might behoove you to read about the import
statement.
您可能应该阅读有关该import
声明的信息。
回答by Marcin
You're not importing defaultdict
. Do either:
您没有导入defaultdict
. 执行以下任一操作:
from collections import defaultdict
or
或者
import collections
d = collections.defaultdict(list)
回答by peascloud
You need to write:
你需要写:
from collections import defaultdict