Add counts from two counters. >>> Counter('abbb') + Counter('bcc') Counter({'b': 4, 'c': 2, 'a': 1})
(self, other)
| 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. |