0

I'm using collections.Counter. It's great so far, except that I'd like it to count objects of the same class as the same. How can I do this? I tried overriding the hash() method on the class so that all instances would hash the same. Right now, if I do Counter([Type1(), Type1(), Type2(), Type2()]) it will return {<Type1 object at...>:1,<Type1 object at...>:1,<Type2 object at...>:1,<Type2 object at...>:1} I would prefer it to return something like {"Type1":2, "Type2":2} instead. Is this possible? I was poking around the docs and couldn't figure out how to make it work.

I should add that the classes I'm using are essentially wrappers for constant values. It's just more convenient to wrap them in a class. Under no circumstances will one Type1 object ever differ from another Type1 object.

3 Answers 3

3

How about this?

Counter(type(x) for x in [Type1(), Type1(), Type2(), Type2()])

This is how you would use it:

>>> type_counter = Counter(type(x) for x in [Type1(), Type1(), Type2(), Type2()])
>>> type_counter
Counter({<class '__main__.Type2'>: 2, <class '__main__.Type1'>: 2})
>>> type_counter[Type1]
2
>>> type_counter[type(Type2())]
2
1
  • This is the best way. Classes can be used as dictionary keys!
    – kindall
    Sep 9, 2013 at 23:37
1

After having read your question again, I'm adding a different approach which might be more suitable to your needs.

A Counter is a dictionary, and dictionaries in Python use the __hash__ method and the __eq__ method to compare objects. So you need to define these two methods if you want objects which always compare equal and can be used as dictionary keys.

# (Python 3)
class Type1:
    def __eq__(self, other):
        if isinstance(other, Type1):
            return True
        return super().__eq__(other)

    def __hash__(self):
        return 1329916036    # anything constant

If you do the same for Type2, you can count instances in a Counter like this:

>>> mycounter = Counter([Type1(), Type1(), Type2(), Type2()])
>>> mycounter
Counter({<__main__.Type1 object at ...>: 2, <__main__.Type2 object at ...>: 2})
>>> mycounter[Type1()]
2
1
  • Ah, eq, that's what I was missing. Thank you. I actually think the approach you suggested works better for my particular situation, though this is what I was asking for! Sep 9, 2013 at 23:45
1

If you want to group them by class name, you can use the __name__ attribute:

Counter(i.__name__ for i in (Type1(), Type2(), Type1()))

Or:

from operator import attrgetter

Counter(map(attrgetter('__name__'), (Type1(), Type2(), Type1())))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.