diff options
author | Raymond Hettinger <python@rcn.com> | 2010-04-03 10:32:58 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2010-04-03 10:32:58 (GMT) |
commit | 9c01e441bb2589853446787d4150fe623050a1e1 (patch) | |
tree | fe8479098709faeb7bfd9c7e4b47091c6e0e0212 /Doc | |
parent | 03d788dc4d6d399ba1092da5895edd7e6900345a (diff) | |
download | cpython-9c01e441bb2589853446787d4150fe623050a1e1.zip cpython-9c01e441bb2589853446787d4150fe623050a1e1.tar.gz cpython-9c01e441bb2589853446787d4150fe623050a1e1.tar.bz2 |
Add a subtract() method to collections.Counter()
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/collections.rst | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index 176975b..aa17a55 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -214,6 +214,17 @@ For example:: >>> Counter('abracadabra').most_common(3) [('a', 5), ('r', 2), ('b', 2)] + .. method:: subtract([iterable-or-mapping]) + + Elements are subtracted from an *iterable* or from another *mapping* + (or counter). Like :meth:`dict.update` but subtracts counts instead + of replacing them. Both inputs and outputs may be zero or negative. + + >>> c = Counter(a=4, b=2, c=0, d=-2) + >>> d = Counter(a=1, b=2, c=3, d=4) + >>> c.subtract(d) + Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6}) + The usual dictionary methods are available for :class:`Counter` objects except for two which work differently for counters. |