Intersection is the minimum of corresponding counts. >>> Counter('abbb') & Counter('bcc') Counter({'b': 1})
(self, other)
| 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 | |
| 187 | if __name__ == '__main__': |