Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name Counter

Tags:

python

I have tried using Counter() but everytime I do:

from collections import Counter

I get an error saying:

Traceback (most recent call last):
  File "<web session>", line 1, in <module>
ImportError: cannot import name Counter

Do I actually have to make a file that has counter in it and then import it from there or something? I am a beginner so only the most basic answer will do.

like image 690
Keely Aranyos Avatar asked Apr 11 '12 02:04

Keely Aranyos


3 Answers

Counter is only supported python2.7 and higher and is not available in earlier versions.

like image 143
sharafjaffri Avatar answered Nov 02 '22 00:11

sharafjaffri


Use

from collections import Counter

and be sure that the C letter of Counter is a capital letter.

like image 9
Mostafa Elnady Avatar answered Nov 02 '22 00:11

Mostafa Elnady


You can just cast the list to a set instead:

l = ['a','b', 'c', 'a', 'd', 'e', 's', 'd', 'e', 'c']
print (len(set(l)) #prints  6
like image 2
Jacob Avatar answered Nov 01 '22 23:11

Jacob