Subtract count, but keep only results with positive counts. >>> Counter('abbbc') - Counter('bccd') Counter({'b': 2, 'a': 1})
(self, other)
| 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. |