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

Method __sub__

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

Subtract count, but keep only results with positive counts. >>> Counter('abbbc') - Counter('bccd') Counter({'b': 2, 'a': 1})

(self, other)

Source from the content-addressed store, hash-verified

132 return result
133
134 def __sub__(self, other):
135 ''' Subtract count, but keep only results with positive counts.
136
137 >>> Counter('abbbc') - Counter('bccd')
138 Counter({'b': 2, 'a': 1})
139
140 '''
141 if not isinstance(other, Counter):
142 return NotImplemented
143 result = Counter()
144 for elem in set(self) | set(other):
145 newcount = self[elem] - other[elem]
146 if newcount > 0:
147 result[elem] = newcount
148 return result
149
150 def __or__(self, other):
151 '''Union is the maximum of value in either of the input counters.

Callers

nothing calls this directly

Calls 2

CounterClass · 0.70
setFunction · 0.50

Tested by

no test coverage detected