2011-06-19 42 views
9

我一直在嘗試在3.2中使用計數器,但我不確定是否嘗試正確使用它。任何想法,爲什麼我得到錯誤?在Python 3.2中使用「計數器」

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

我似乎可以訪問計數器,如果我去「collections.Counter()」但不是在文檔中的示例。

回答

21

您想要from collections import Counter。使用import collections只會使集合中的東西成爲集合。 東西。更多關於this tutorial chapter前幾部分中import的模塊和工作原理。