>> import collections >>> Counter()-6ren">
gpt4 book ai didi

python - 在 Python 3.2 中使用 "Counter"

转载 作者:IT老高 更新时间:2023-10-28 20:59:45 25 4
gpt4 key购买 nike

我一直在尝试在 Python 3.2 中使用 Counter 方法,但我不确定我是否正确使用它。知道为什么我会收到错误消息吗?

>>> import collections
>>> Counter()
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
Counter()
NameError: name 'Counter' is not defined

如果我使用 collections.Counter(),我可以访问 Counter,但不能访问文档中的示例。

最佳答案

你想要 from collections import Counter。使用 import collections 只会使集合中的内容可作为集合使用。something。在 this tutorial chapter 的前几节中详细了解模块和 import 的工作原理.

关于python - 在 Python 3.2 中使用 "Counter",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6400538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com