summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2022-02-02 04:18:11 (GMT)
committerGitHub <noreply@github.com>2022-02-02 04:18:11 (GMT)
commitf77beacf015e9bf8762e9b9a9a631396b05d4d9a (patch)
tree08c4c61e51bcb1c620fbbda45f2526e0e1fbdd0d /Doc
parentabcc3d75f6e570519cb37c62130a2295c6928bff (diff)
downloadcpython-f77beacf015e9bf8762e9b9a9a631396b05d4d9a.zip
cpython-f77beacf015e9bf8762e9b9a9a631396b05d4d9a.tar.gz
cpython-f77beacf015e9bf8762e9b9a9a631396b05d4d9a.tar.bz2
Fix minor details in the Counter docs (GH-31029)
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/collections.rst13
1 files changed, 10 insertions, 3 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index b8a717d..b97bc42 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -271,7 +271,7 @@ For example::
.. versionadded:: 3.1
.. versionchanged:: 3.7 As a :class:`dict` subclass, :class:`Counter`
- Inherited the capability to remember insertion order. Math operations
+ inherited the capability to remember insertion order. Math operations
on *Counter* objects also preserve order. Results are ordered
according to when an element is first encountered in the left operand
and then by the order encountered in the right operand.
@@ -366,19 +366,26 @@ Several mathematical operations are provided for combining :class:`Counter`
objects to produce multisets (counters that have counts greater than zero).
Addition and subtraction combine counters by adding or subtracting the counts
of corresponding elements. Intersection and union return the minimum and
-maximum of corresponding counts. Each operation can accept inputs with signed
+maximum of corresponding counts. Equality and inclusion compare
+corresponding counts. Each operation can accept inputs with signed
counts, but the output will exclude results with counts of zero or less.
+.. doctest::
+
>>> c = Counter(a=3, b=1)
>>> d = Counter(a=1, b=2)
>>> c + d # add two counters together: c[x] + d[x]
Counter({'a': 4, 'b': 3})
>>> c - d # subtract (keeping only positive counts)
Counter({'a': 2})
- >>> c & d # intersection: min(c[x], d[x]) # doctest: +SKIP
+ >>> c & d # intersection: min(c[x], d[x])
Counter({'a': 1, 'b': 1})
>>> c | d # union: max(c[x], d[x])
Counter({'a': 3, 'b': 2})
+ >>> c == d # equality: c[x] == d[x]
+ False
+ >>> c <= d # inclusion: c[x] <= d[x]
+ False
Unary addition and subtraction are shortcuts for adding an empty counter
or subtracting from an empty counter.