collections - 集合模块 Python 中的计数器

标签 collections counter python-import importerror python-2.6

我遇到了一个非常奇怪的问题。我正在尝试在集合模块中使用计数器功能。但是,我不断收到相同的错误消息

AttributeError: 'module' object has no attribute 'Counter'

我之前尝试过使用它并且效果很好,但是现在由于某种原因,当我导入“集合”模块时,它的属性数量非常有限。

我试过了:
import collections   # when calling Counter I would then use collections.Counter()
import collections as collect # collect.Counter()

对于这两个我不断收到属性错误。

我也试过
from collections import Counter

在这种情况下,我得到:
ImportError: cannot import name Counter

这些都在 ipython 界面和脚本中进行了测试(不导入其他任何东西,只是集合)。

有任何想法吗?

最佳答案

您应该使用新版本的 python AS python 3。然后您可以使用此模块。
然后导入,

import collections
from collections import counter

关于collections - 集合模块 Python 中的计数器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13311094/

相关文章:

python - Python 模块的绝对与显式相对导入

c# - 为什么要使用 ICollection<T> 而不是 List<T>,如果我使用 ICollection<T>,那么如何使用 AddRange、Insert 和其他列表方法

c - C 中的位计数类似于 bit twiddling hack

python - 在 python 3.4 中将字符串日期转换为时间戳

javascript - 如何以每秒一定的速率向上计数(并在计数时显示数字)到无穷大?

java - Java中的计数器循环

python - 在 Python 3 包和脚本中导入的最佳实践

java - hashCode()、equals() 在维护 Set 中用户定义对象的唯一性时的行为?

java - Collections.sort() 不适用于自定义对象

java - 集合框架中的接口(interface)