Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用;柜台;在Python 3.2中_Python_Python 3.x - Fatal编程技术网

使用;柜台;在Python 3.2中

使用;柜台;在Python 3.2中,python,python-3.x,Python,Python 3.x,我一直试图在Python3.2中使用计数器方法,但我不确定是否正确使用了它。知道我为什么会出错吗 >>> import collections >>> Counter() Traceback (most recent call last): File "<pyshell#5>", line 1, in <module> Counter() NameError: name 'Counter' is not defined 导

我一直试图在Python3.2中使用
计数器
方法,但我不确定是否正确使用了它。知道我为什么会出错吗

>>> import collections
>>> Counter()
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    Counter()
NameError: name 'Counter' is not defined
导入集合 >>>计数器() 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 计数器() NameError:未定义名称“计数器”
如果我转到
collections.Counter()
,我可以访问
计数器
,但不能访问文档中的示例。

您希望
从集合导入计数器
。使用
import collections
只能使集合中的内容作为collections.something可用。更多关于模块和导入工作的信息,请参见的前几节。

使用计数器试试它的工作原理

import collections
print collections.Counter(['a','b','c','a','b','b'])
输出:

Counter({'b': 3, 'a': 2, 'c': 1})

它看起来不像Python3.2,它看起来像是2.7或2.x标题是“在Python3.2中使用计数器”。这又给公认的答案增加了什么?请阅读