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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:09:08  来源:igfitidea点击:

defaultdict is not defined

pythonpython-3.x

提问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 importstatement.

您可能应该阅读有关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