Tweak the docs for Counter() objects.
This commit is contained in:
parent
8184f5a46c
commit
939a3cc5a2
|
@ -156,14 +156,14 @@ Notes on using :class:`Set` and :class:`MutableSet` as a mixin:
|
||||||
A counter tool is provided to support convenient and rapid tallies.
|
A counter tool is provided to support convenient and rapid tallies.
|
||||||
For example::
|
For example::
|
||||||
|
|
||||||
# Tally occurrences of words in a list
|
>>> # Tally occurrences of words in a list
|
||||||
>>> cnt = Counter()
|
>>> cnt = Counter()
|
||||||
>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
|
>>> for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
|
||||||
... cnt[word] += 1
|
... cnt[word] += 1
|
||||||
>>> cnt
|
>>> cnt
|
||||||
Counter({'blue': 3, 'red': 2, 'green': 1})
|
Counter({'blue': 3, 'red': 2, 'green': 1})
|
||||||
|
|
||||||
# Find the ten most common words in Hamlet
|
>>> # Find the ten most common words in Hamlet
|
||||||
>>> import re
|
>>> import re
|
||||||
>>> words = re.findall('\w+', open('hamlet.txt').read().lower())
|
>>> words = re.findall('\w+', open('hamlet.txt').read().lower())
|
||||||
>>> Counter(words).most_common(10)
|
>>> Counter(words).most_common(10)
|
||||||
|
@ -256,8 +256,8 @@ Several multiset mathematical operations are provided for combining
|
||||||
contain repeated elements (with counts of one or more). Addition and
|
contain repeated elements (with counts of one or more). Addition and
|
||||||
subtraction combine counters by adding or subtracting the counts of
|
subtraction combine counters by adding or subtracting the counts of
|
||||||
corresponding elements. Intersection and union return the minimum and maximum
|
corresponding elements. Intersection and union return the minimum and maximum
|
||||||
of corresponding counts. All four multiset operations exclude results with
|
of corresponding counts. Each operation can accept inputs with signed counts,
|
||||||
counts less than one::
|
but the output excludes results with counts less than one.
|
||||||
|
|
||||||
>>> c = Counter(a=3, b=1)
|
>>> c = Counter(a=3, b=1)
|
||||||
>>> d = Counter(a=1, b=2)
|
>>> d = Counter(a=1, b=2)
|
||||||
|
|
Loading…
Reference in New Issue