Python 集合模块中的计数器

2024-01-10

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

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 界面和脚本中进行了测试(不导入任何其他内容,仅导入集合)。

有任何想法吗?


The Counter在 Python 2.7 中,类被添加到模块中。您很可能使用 Python 2.6 或更早版本。来自collections.Counter()文档 http://docs.python.org/2/library/collections.html#collections.Counter:

2.7 版本中的新功能。

在 python 2.5 或 2.6 上,使用这个向后移植 http://code.activestate.com/recipes/576611-counter-class/反而。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 集合模块中的计数器 的相关文章

随机推荐