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

Method __or__

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

Union is the maximum of value in either of the input counters. >>> Counter('abbb') | Counter('bcc') Counter({'b': 3, 'c': 2, 'a': 1})

(self, other)

Source from the content-addressed store, hash-verified

148 return result
149
150 def __or__(self, other):
151 '''Union is the maximum of value in either of the input counters.
152
153 >>> Counter('abbb') | Counter('bcc')
154 Counter({'b': 3, 'c': 2, 'a': 1})
155
156 '''
157 if not isinstance(other, Counter):
158 return NotImplemented
159 _max = max
160 result = Counter()
161 for elem in set(self) | set(other):
162 newcount = _max(self[elem], other[elem])
163 if newcount > 0:
164 result[elem] = newcount
165 return result
166
167 def __and__(self, other):
168 ''' Intersection is the minimum of corresponding counts.

Callers

nothing calls this directly

Calls 2

CounterClass · 0.70
setFunction · 0.50

Tested by

no test coverage detected