negative "counts" in collections.Counter?

Vlastimil Brom vlastimil.brom at gmail.com
Sun Mar 7 16:04:42 EST 2010


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).

This is simple enough to do with own code, but I thought, this would
be exactly the case for Counter...
However, as the Counter only returns positive counts, one has to get
the difference in both directions and combine them afterwards, maybe
something like:

>>> c1=collections.Counter("aabcddd")
>>> c2=collections.Counter("abbbd")
>>> added_c2 = c2-c1
>>> removed_c2 = c1-c2
>>> negative_added_c2 = dict((k, v*-1) for (k, v) in removed_c2.items())
>>> changed_c2 = dict(added_c2)
>>> changed_c2.update(negative_added_c2)
>>> changed_c2
{'a': -1, 'c': -1, 'b': 2, 'd': -2}
>>>

It seems to me, that with negative counts allowed in Counter, this
would simply be the matter of a single difference:
changed_c2 = c2 - c1

Is there a possibility to make the Counter work this way (other than
replacing its methods in a subclass, which might be comparable to
writing the naive counting class itself)?
Are there maybe some reasons I missed to disable negative counts here?
(As I could roughly understand, the Counter isn't quite limited to the
mathematical notion of multiset; it seems to accept negative counts,
but its methods only output the positive part).
Is this kind of task - a comparison in both directions - an unusual
one, or is it simply not the case for Counter?

Thanks in advance,
    vbr



More information about the Python-list mailing list