使用;柜台;在Python 3.2中
我一直试图在Python3.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 导
计数器
方法,但我不确定是否正确使用了它。知道我为什么会出错吗
>>> 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中使用计数器”。这又给公认的答案增加了什么?请阅读