Skip to content

Commit f77beac

Browse files
authored
Fix minor details in the Counter docs (GH-31029)
1 parent abcc3d7 commit f77beac

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

Doc/library/collections.rst

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ For example::
271271
.. versionadded:: 3.1
272272

273273
.. versionchanged:: 3.7 As a :class:`dict` subclass, :class:`Counter`
274-
Inherited the capability to remember insertion order. Math operations
274+
inherited the capability to remember insertion order. Math operations
275275
on *Counter* objects also preserve order. Results are ordered
276276
according to when an element is first encountered in the left operand
277277
and then by the order encountered in the right operand.
@@ -366,19 +366,26 @@ Several mathematical operations are provided for combining :class:`Counter`
366366
objects to produce multisets (counters that have counts greater than zero).
367367
Addition and subtraction combine counters by adding or subtracting the counts
368368
of corresponding elements. Intersection and union return the minimum and
369-
maximum of corresponding counts. Each operation can accept inputs with signed
369+
maximum of corresponding counts. Equality and inclusion compare
370+
corresponding counts. Each operation can accept inputs with signed
370371
counts, but the output will exclude results with counts of zero or less.
371372

373+
.. doctest::
374+
372375
>>> c = Counter(a=3, b=1)
373376
>>> d = Counter(a=1, b=2)
374377
>>> c + d # add two counters together: c[x] + d[x]
375378
Counter({'a': 4, 'b': 3})
376379
>>> c - d # subtract (keeping only positive counts)
377380
Counter({'a': 2})
378-
>>> c & d # intersection: min(c[x], d[x]) # doctest: +SKIP
381+
>>> c & d # intersection: min(c[x], d[x])
379382
Counter({'a': 1, 'b': 1})
380383
>>> c | d # union: max(c[x], d[x])
381384
Counter({'a': 3, 'b': 2})
385+
>>> c == d # equality: c[x] == d[x]
386+
False
387+
>>> c <= d # inclusion: c[x] <= d[x]
388+
False
382389

383390
Unary addition and subtraction are shortcuts for adding an empty counter
384391
or subtracting from an empty counter.

Lib/collections/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,10 @@ def __repr__(self):
736736
# To strip negative and zero counts, add-in an empty counter:
737737
# c += Counter()
738738
#
739+
# Results are ordered according to when an element is first
740+
# encountered in the left operand and then by the order
741+
# encountered in the right operand.
742+
#
739743
# When the multiplicities are all zero or one, multiset operations
740744
# are guaranteed to be equivalent to the corresponding operations
741745
# for regular sets.

0 commit comments

Comments
 (0)