diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2019-02-21 17:19:00 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-02-21 17:19:00 (GMT) |
commit | 407c7343266eb3e5a2f5c1f4913082c84f8dd8a0 (patch) | |
tree | ec7a7cf9dc5bbcd4ecdd1b92475e6953632c67fd /Doc/library/collections.rst | |
parent | 86f093f71a594dcaf21b67ba13dda72863e9bde9 (diff) | |
download | cpython-407c7343266eb3e5a2f5c1f4913082c84f8dd8a0.zip cpython-407c7343266eb3e5a2f5c1f4913082c84f8dd8a0.tar.gz cpython-407c7343266eb3e5a2f5c1f4913082c84f8dd8a0.tar.bz2 |
bpo-36057 Update docs and tests for ordering in collections.Counter [no behavior change] (#11962)
* Add tests for Counter order. No behavior change.
* Update docs and tests
* Fix doctest output and capitalization
Diffstat (limited to 'Doc/library/collections.rst')
-rw-r--r-- | Doc/library/collections.rst | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index ca2f116..1a09379 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -268,6 +268,11 @@ For example:: .. versionadded:: 3.1 + .. versionchanged:: 3.7 As a :class:`dict` subclass, :class:`Counter` + 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. Counter objects support three methods beyond those available for all dictionaries: @@ -275,8 +280,8 @@ For example:: .. method:: elements() Return an iterator over elements repeating each as many times as its - count. Elements are returned in arbitrary order. If an element's count - is less than one, :meth:`elements` will ignore it. + count. Elements are returned in the order first encountered. If an + element's count is less than one, :meth:`elements` will ignore it. >>> c = Counter(a=4, b=2, c=0, d=-2) >>> sorted(c.elements()) @@ -287,10 +292,10 @@ For example:: Return a list of the *n* most common elements and their counts from the most common to the least. If *n* is omitted or ``None``, :meth:`most_common` returns *all* elements in the counter. - Elements with equal counts are ordered arbitrarily: + Elements with equal counts are ordered in the order first encountered: - >>> Counter('abracadabra').most_common(3) # doctest: +SKIP - [('a', 5), ('r', 2), ('b', 2)] + >>> Counter('abracadabra').most_common(3) + [('a', 5), ('b', 2), ('r', 2)] .. method:: subtract([iterable-or-mapping]) |