Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
9a496fb2
Commit
9a496fb2
authored
Jan 27, 2009
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tweak column alignment in collections docs.
parent
b9b52253
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
33 deletions
+26
-33
Doc/library/collections.rst
Doc/library/collections.rst
+26
-33
No files found.
Doc/library/collections.rst
View file @
9a496fb2
...
@@ -171,10 +171,10 @@ For example::
...
@@ -171,10 +171,10 @@ For example::
Elements are counted from an *iterable* or initialized from another
Elements are counted from an *iterable* or initialized from another
*mapping* (or counter)::
*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
>>> c = Counter({'red': 4, 'blue': 2})
# a new counter from a mapping
>>> c = Counter({'red': 4, 'blue': 2})
# a new counter from a mapping
>>> c = Counter(spam=8, eggs=1)
# a new counter from keyword args
>>> c = Counter(cats=4, dogs=8)
# a new counter from keyword args
Counter objects have a dictionary interface except that they return a zero
Counter objects have a dictionary interface except that they return a zero
count for missing items instead of raising a :exc:`KeyError`::
count for missing items instead of raising a :exc:`KeyError`::
...
@@ -207,10 +207,10 @@ For example::
...
@@ -207,10 +207,10 @@ For example::
.. method:: most_common([n])
.. method:: most_common([n])
Return a list of the *n* most common elements and their counts from
Return a list of the *n* most common elements and their counts from
the
the most common to the least. If *n* is not specified or is ``None``,
most common to the least. If *n* not specified, :func:`most_common`
return
a list of all element counts in decreasing order of frequency.
return
s *all* elements in the counter. Elements with equal counts are
Elements with equal counts are
ordered arbitrarily::
ordered arbitrarily::
>>> Counter('abracadabra').most_common(3)
>>> Counter('abracadabra').most_common(3)
[('a', 5), ('r', 2), ('b', 2)]
[('a', 5), ('r', 2), ('b', 2)]
...
@@ -220,33 +220,26 @@ For example::
...
@@ -220,33 +220,26 @@ For example::
.. method:: fromkeys(iterable)
.. method:: fromkeys(iterable)
This class method is not implemented for :class:`Counter` objects.
This class method is not implemented for :class:`Counter` objects.
.. method:: update([iterable-or-mapping])
.. method:: update([iterable-or-mapping])
Elements are counted from an *iterable* or added-in from another
Elements are counted from an *iterable* or added-in from another
*mapping* (or counter). Like :meth:`dict.update` but adds-in counts
*mapping* (or counter). Like :meth:`dict.update` but adds counts
instead of replacing them. Also, the *iterable* is expected to be a
instead of replacing them. Also, the *iterable* is expected to be a
sequence of elements, not a sequence of ``(key, value)`` pairs::
sequence of elements, not a sequence of ``(key, value)`` pairs.
>>> c = Counter('which')
>>> c.update('witch') # add elements from another iterable
>>> d = Counter('watch')
>>> c.update(d) # add elements from another counter
>>> c['h'] # four 'h' in which, witch, and watch
4
Common patterns for working with :class:`Counter` objects::
Common patterns for working with :class:`Counter` objects::
sum(c.values()) # total of all counts
sum(c.values())
# total of all counts
c.clear() # reset all counts
c.clear()
# reset all counts
list(c) # list unique elements
list(c)
# list unique elements
set(c) # convert to a set
set(c)
# convert to a set
dict(c) # convert to a regular dictionary
dict(c)
# convert to a regular dictionary
c.items() # convert to a list of (elem, cnt) pairs
c.items()
# convert to a list of (elem, cnt) pairs
Counter(dict(list_of_pairs)) # convert from a list of (elem, cnt) pairs
Counter(dict(list_of_pairs))
# convert from a list of (elem, cnt) pairs
c.most_common()[:-n:-1] # n least common elements
c.most_common()[:-n:-1]
# n least common elements
c += Counter() # remove zero and negative counts
c += Counter()
# remove zero and negative counts
Several multiset mathematical operations are provided for combining
Several multiset mathematical operations are provided for combining
:class:`Counter` objects. Multisets are like regular sets but are allowed to
:class:`Counter` objects. Multisets are like regular sets but are allowed to
...
@@ -258,13 +251,13 @@ counts less than one::
...
@@ -258,13 +251,13 @@ 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)
>>> c + d
# add two counters together: c[x] + d[x]
>>> c + d # add two counters together: c[x] + d[x]
Counter({'a': 4, 'b': 3})
Counter({'a': 4, 'b': 3})
>>> c - d
# subtract (keeping only positive counts)
>>> c - d # subtract (keeping only positive counts)
Counter({'a': 2})
Counter({'a': 2})
>>> c & d
# intersection: min(c[x], d[x])
>>> c & d # intersection: min(c[x], d[x])
Counter({'a': 1, 'b': 1})
Counter({'a': 1, 'b': 1})
>>> c | d
# union: max(c[x], d[x])
>>> c | d # union: max(c[x], d[x])
Counter({'a': 3, 'b': 2})
Counter({'a': 3, 'b': 2})
.. seealso::
.. seealso::
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment