mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Add a subtract() method to collections.Counter()
This commit is contained in:
parent
03d788dc4d
commit
9c01e441bb
4 changed files with 51 additions and 0 deletions
|
@ -436,6 +436,34 @@ class Counter(dict):
|
|||
if kwds:
|
||||
self.update(kwds)
|
||||
|
||||
def subtract(self, iterable=None, **kwds):
|
||||
'''Like dict.update() but subtracts counts instead of replacing them.
|
||||
Counts can be reduced below zero. Both the inputs and outputs are
|
||||
allowed to contain zero and negative counts.
|
||||
|
||||
Source can be an iterable, a dictionary, or another Counter instance.
|
||||
|
||||
>>> c = Counter('which')
|
||||
>>> c.subtract('witch') # subtract elements from another iterable
|
||||
>>> c.subtract(Counter('watch')) # subtract elements from another counter
|
||||
>>> c['h'] # 2 in which, minus 1 in witch, minus 1 in watch
|
||||
0
|
||||
>>> c['w'] # 1 in which, minus 1 in witch, minus 1 in watch
|
||||
-1
|
||||
|
||||
'''
|
||||
if iterable is not None:
|
||||
if isinstance(iterable, Mapping):
|
||||
self_get = self.get
|
||||
for elem, count in iterable.items():
|
||||
self[elem] = self_get(elem, 0) - count
|
||||
else:
|
||||
self_get = self.get
|
||||
for elem in iterable:
|
||||
self[elem] = self_get(elem, 0) - 1
|
||||
if kwds:
|
||||
self.subtract(kwds)
|
||||
|
||||
def copy(self):
|
||||
'Like dict.copy() but returns a Counter instance instead of a dict.'
|
||||
return Counter(self)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue