[issue10667] collections.Counter object in C

Justin Peel report at bugs.python.org
Sat Dec 11 01:26:45 CET 2010


Justin Peel <peelpy at gmail.com> added the comment:

I've done as Antoine asked and made a pure Python PyCounter class and a C-enhanced Counter class that both use the mixin CounterBase class. I also added to the tests so that both PyCounter and Counter are tested. I left the update_fromsubs() method in Counter for now, but haven't made a Python equivalent function for PyCounter in case the method is rejected.

Also, I realized that my build was still in debug mode so those benchmark weren't quite accurate (Oops). The Counter class is only 4-6x faster for most cases. That's still good, but not quite as good as I'd previously reported.

Lastly, I thought of one more method that could be quite useful. Say that you have a list like [('apples',4),('oranges',5),('apples',8)] where the second element in each tuple indicates how many of them there are. The resultant Counter should have {'apples': 12, 'oranges':5}. We don't really have a good way to count this, but we could add a small method like the following:

    def fromitems(self, items):
        for key, value in items:
            self[key] += value

and I could easily make a C function for it. I think that this simple method could be very useful to some people. What do you all think of this addition?

----------
Added file: http://bugs.python.org/file20004/counterpatch2.diff

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue10667>
_______________________________________


More information about the Python-bugs-list mailing list