MCPcopy Create free account
hub / github.com/ActiveState/code / __add__

Method __add__

recipes/Python/577664_Counter_class/recipe-577664.py:117–132  ·  view source on GitHub ↗

Add counts from two counters. >>> Counter('abbb') + Counter('bcc') Counter({'b': 4, 'c': 2, 'a': 1})

(self, other)

Source from the content-addressed store, hash-verified

115 # c += Counter()
116
117 def __add__(self, other):
118 '''Add counts from two counters.
119
120 >>> Counter('abbb') + Counter('bcc')
121 Counter({'b': 4, 'c': 2, 'a': 1})
122
123
124 '''
125 if not isinstance(other, Counter):
126 return NotImplemented
127 result = Counter()
128 for elem in set(self) | set(other):
129 newcount = self[elem] + other[elem]
130 if newcount > 0:
131 result[elem] = newcount
132 return result
133
134 def __sub__(self, other):
135 ''' Subtract count, but keep only results with positive counts.

Callers

nothing calls this directly

Calls 2

CounterClass · 0.70
setFunction · 0.50

Tested by

no test coverage detected