0

I'm trying to understand this behavior:

>>> Counter({'a':0})
Counter({'a': 0})
>>> Counter({'a':0}) + Counter({'a':0})
Counter()
>>> len(Counter({'a':0}))
1
>>> len(Counter({'a':0}) + Counter({'a':0}))
0

This seems inconsistent to me -- thoughts?

Bill
  • 1,247
  • 8
  • 12
  • 5
    The documentation is quite explicit about this; keys with count 0 are dropped when summing two Counters. – Martijn Pieters Aug 08 '14 at 17:37
  • I looked for documentation on this behavior and couldn't find it, here's where I was looking. Any other suggestions where to look? https://docs.python.org/2/library/collections.html – Bill Aug 08 '14 at 17:41
  • 1
    A quote from that page: *"Addition and subtraction combine counters by adding or subtracting the counts of corresponding elements. Intersection and union return the minimum and maximum of corresponding counts. Each operation can accept inputs with signed counts, but the output will **exclude results with counts of zero or less**."* – jonrsharpe Aug 08 '14 at 17:44
  • It is documented on that page; my answer to the linked duplicate highlights the exact phrasing. – Martijn Pieters Aug 08 '14 at 17:44

1 Answers1

0

No it is not inconsistent. While summing two Counter classes, negative and zero counts are ignored. Go through the Notes section in this for more details: https://docs.python.org/2/library/collections.html#collections.Counter

sgp
  • 1,738
  • 6
  • 17
  • 31