mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Minor documentation tweaks and simpler update() example.
This commit is contained in:
parent
5a0c864045
commit
8278385a05
1 changed files with 11 additions and 14 deletions
|
@ -175,7 +175,7 @@ For example::
|
||||||
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
|
[('the', 1143), ('and', 966), ('to', 762), ('of', 669), ('i', 631),
|
||||||
('you', 554), ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
|
('you', 554), ('a', 546), ('my', 514), ('hamlet', 471), ('in', 451)]
|
||||||
|
|
||||||
.. class:: Counter([iterable])
|
.. class:: Counter([iterable-or-mapping])
|
||||||
|
|
||||||
A :class:`Counter` is a :class:`dict` subclass for counting hashable items.
|
A :class:`Counter` is a :class:`dict` subclass for counting hashable items.
|
||||||
It is an unordered collection where elements are stored as dictionary keys
|
It is an unordered collection where elements are stored as dictionary keys
|
||||||
|
@ -183,8 +183,8 @@ For example::
|
||||||
any integer value including zero or negative counts. The :class:`Counter`
|
any integer value including zero or negative counts. The :class:`Counter`
|
||||||
class is similar to bags or multisets in other languages.
|
class is similar to bags or multisets in other languages.
|
||||||
|
|
||||||
Elements are counted from the *iterable* if given. Also, the counts
|
Elements are counted from an *iterable* or initialized from another
|
||||||
can be initialized from another mapping of elements to their counts::
|
*mapping* (or counter)::
|
||||||
|
|
||||||
>>> c = Counter() # a new, empty counter
|
>>> c = Counter() # a new, empty counter
|
||||||
>>> c = Counter('gallahad') # a new counter from an iterable
|
>>> c = Counter('gallahad') # a new counter from an iterable
|
||||||
|
@ -244,21 +244,18 @@ For example::
|
||||||
There is no equivalent class method for :class:`Counter` objects.
|
There is no equivalent class method for :class:`Counter` objects.
|
||||||
Raises a :exc:`NotImplementedError` when called.
|
Raises a :exc:`NotImplementedError` when called.
|
||||||
|
|
||||||
.. method:: update(iterable)
|
.. method:: update([iterable-or-mapping])
|
||||||
|
|
||||||
Like :meth:`dict.update` but adds-in counts instead of replacing them.
|
Like :meth:`dict.update` but adds-in counts instead of replacing them.
|
||||||
|
|
||||||
Elements are counted from the *iterable* if given. Also, the counts
|
Elements are counted from an *iterable* or added-in from another
|
||||||
can be taken from another counter or mapping of elements to their
|
*mapping* (or counter)::
|
||||||
counts::
|
|
||||||
|
|
||||||
>>> c = Counter('which') # count letters in a word
|
>>> c = Counter('which')
|
||||||
>>> d = Counter('witch') # count letters in another word
|
>>> c.update('witch') # add elements from another iterable
|
||||||
>>> c.update(d) # add counts from d to those in c
|
>>> d = Counter('watch')
|
||||||
>>> c['h'] # count of 'h' is now three
|
>>> c.update(d) # add elements from another counter
|
||||||
3
|
>>> c['h'] # four 'h' in which, witch, and watch
|
||||||
>>> c.update('watch') # add in letters from another word
|
|
||||||
>>> c['h'] # count of 'h' is now four
|
|
||||||
4
|
4
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue