Counter in Collections module Python

后端 未结 4 1671
耶瑟儿~
耶瑟儿~ 2020-11-29 10:17

I\'ve come across a really weird problem. I\'m trying to use Counter function in collections module. However, I keep getting the same error message

Attribut         


        
相关标签:
4条回答
  • 2020-11-29 11:00

    Came across the same issue while installing pandas.

    Cause: Counter is only supported in python2.7 and higher and is not available in earlier versions - Counter class got added into collections package in Python 2.7.


    Solution 1: As stated by Martin Pieters - use the backport.

    Add counter.py at /lib64/python2.6/ - this is where the collections.py is ./lib64/python2.6/collections.py Patch collections.py with:

    from counter import Counter
    

    Solution 2: use the backport_collections package. Next patch (the import statement) the package you're getting exception at i.e. pandas in my case:

    from backport_collections import Counter
    
    0 讨论(0)
  • 2020-11-29 11:13

    You should use a new version of python AS python 3. You can then use this module. Then import,

    import collections
    from collections import counter
    
    0 讨论(0)
  • 2020-11-29 11:15

    You're probably using an old version of Python, the Counter class, as stated in the documentation was added in version 2.7.

    0 讨论(0)
  • 2020-11-29 11:16

    The Counter class was added to the module in Python 2.7. You are most likely using Python 2.6 or older. From the collections.Counter() documentation:

    New in version 2.7.

    On python 2.5 or 2.6, use this backport instead.

    0 讨论(0)
提交回复
热议问题