negative "counts" in collections.Counter?

Steven D'Aprano steve at
Mon Mar 8 02:46:28 CET 2010

On Sun, 07 Mar 2010 22:21:27 +0000, Arnaud Delobelle wrote:

> Vlastimil Brom <vlastimil.brom at> writes:
>> Hi all,
>> I'd like to ask about the possibility of negative "counts" in
>> collections.Counter (using Python 3.1); I believe, my usecase is rather
>> trivial, basically I have the word frequencies of two texts and I want
>> do compare them (e.g. to see what was added and removed between
>> different versions of a text).


> Every time I have needed something like collections.Counter, I have
> wanted the behaviour you require too.  As a result, I have never used
> collections.Counter.  Instead I have used plain dictionaries or my own
> class.

Vlastimil and Arnaud,

It looks like Counter already has negative counts in Python 3.1:

>>> import collections
>>> c = collections.Counter({'red': 2, 'green': 0, 'blue': -5})
>>> c['blue'] -= 1
>>> c
Counter({'red': 2, 'green': 0, 'blue': -6})
>>> c['blue'] += 1
>>> c
Counter({'red': 2, 'green': 0, 'blue': -5})

But the + and - operators destroy negative and zero counts:

>>> c + collections.Counter({'blue': 1})
Counter({'red': 2})

I can't imagine what the use-case for that behaviour is.

Given that Counter supports negative counts, it looks to me that the 
behaviour of __add__ and __sub__ is fundamentally flawed. You should 
raise a bug report (feature enhancement) on the bug tracker.

Given that this will change the documented behaviour, it will help if you 
give a short, simple idiom for removing zero and negative elements, 
Arnaud's trick with | Counter().

When you raise the report, please post an update here.


More information about the Python-list mailing list