diff options
author | Raymond Hettinger <python@rcn.com> | 2011-08-09 20:00:40 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2011-08-09 20:00:40 (GMT) |
commit | fcb393c0183c6a8cf47eb9575a1bbe6bc5d236e4 (patch) | |
tree | 51c5d45f70cd9946a373083bd31ddc2f109a9647 /Doc/library/collections.rst | |
parent | 18205baf251495b5a54b08c1f9b0e1763eb27aa1 (diff) | |
download | cpython-fcb393c0183c6a8cf47eb9575a1bbe6bc5d236e4.zip cpython-fcb393c0183c6a8cf47eb9575a1bbe6bc5d236e4.tar.gz cpython-fcb393c0183c6a8cf47eb9575a1bbe6bc5d236e4.tar.bz2 |
Add support for unary plus and unary minus to collections.Counter()
Diffstat (limited to 'Doc/library/collections.rst')
-rw-r--r-- | Doc/library/collections.rst | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index f344b5a..df231f8 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -264,7 +264,7 @@ Common patterns for working with :class:`Counter` objects:: c.items() # convert to a list of (elem, cnt) pairs Counter(dict(list_of_pairs)) # convert from a list of (elem, cnt) pairs c.most_common()[:-n:-1] # n least common elements - c += Counter() # remove zero and negative counts + +c # remove zero and negative counts Several mathematical operations are provided for combining :class:`Counter` objects to produce multisets (counters that have counts greater than zero). @@ -284,6 +284,18 @@ counts, but the output will exclude results with counts of zero or less. >>> c | d # union: max(c[x], d[x]) Counter({'a': 3, 'b': 2}) +Unary addition and substraction are shortcuts for adding an empty counter +or subtracting from an empty counter. + + >>> c = Counter(a=2, b=-4) + >>> +c + Counter({'a': 2}) + >>> -c + Counter({'b': 4}) + +.. versionadded:: 3.3 + Added support for unary plus and unary minus. + .. note:: Counters were primarily designed to work with positive integers to represent |