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

Method __and__

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

Intersection is the minimum of corresponding counts. >>> Counter('abbb') & Counter('bcc') Counter({'b': 1})

(self, other)

Source from the content-addressed store, hash-verified

165 return result
166
167 def __and__(self, other):
168 ''' Intersection is the minimum of corresponding counts.
169
170 >>> Counter('abbb') & Counter('bcc')
171 Counter({'b': 1})
172
173 '''
174 if not isinstance(other, Counter):
175 return NotImplemented
176 _min = min
177 result = Counter()
178 if len(self) < len(other):
179 self, other = other, self
180 for elem in ifilter(self.__contains__, other):
181 newcount = _min(self[elem], other[elem])
182 if newcount > 0:
183 result[elem] = newcount
184 return result
185
186
187if __name__ == '__main__':

Callers

nothing calls this directly

Calls 1

CounterClass · 0.70

Tested by

no test coverage detected