summaryrefslogtreecommitdiffstats
path: root/Doc/library/collections.rst
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2010-04-12 21:47:14 (GMT)
committerRaymond Hettinger <python@rcn.com>2010-04-12 21:47:14 (GMT)
commit4c4d3b1dddb249cd97aec976c5ef7b4ee84db60f (patch)
treee6d8532071a60b30d4db8daad9d8001de6534c79 /Doc/library/collections.rst
parent18628400375cb14130fde1502bd646e997b9a6de (diff)
downloadcpython-4c4d3b1dddb249cd97aec976c5ef7b4ee84db60f.zip
cpython-4c4d3b1dddb249cd97aec976c5ef7b4ee84db60f.tar.gz
cpython-4c4d3b1dddb249cd97aec976c5ef7b4ee84db60f.tar.bz2
Add usage notes for collections.Counter()
Diffstat (limited to 'Doc/library/collections.rst')
-rw-r--r--Doc/library/collections.rst27
1 files changed, 27 insertions, 0 deletions
diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst
index ebb35b1..b19af9c 100644
--- a/Doc/library/collections.rst
+++ b/Doc/library/collections.rst
@@ -258,6 +258,33 @@ counts, but the output will exclude results with counts of zero or less.
>>> c | d # union: max(c[x], d[x])
Counter({'a': 3, 'b': 2})
+.. note::
+
+ Counters were primarily designed to work with positive integers to represent
+ running counts; however, care was taken to not unnecessarily preclude use
+ cases needing other types or negative values. To help with those use cases,
+ this section documents the minimum range and type restrictions.
+
+ * The :class:`Counter` class itself is a dictionary subclass with no
+ restrictions on its keys and values. The values are intended to be numbers
+ representing counts, but you *could* store anything in the value field.
+
+ * The :meth:`most_common` method requires only that the values be orderable.
+
+ * For in-place operations such as ``c[key] += 1``, the value type need only
+ support addition and subtraction. So fractions, floats, and decimals would
+ work and negative values are supported. The same is also true for
+ :meth:`update` and :meth:`subtract` which allow negative and zero values
+ for both inputs and outputs.
+
+ * The multiset methods are designed only for use cases with positive values.
+ The inputs may be negative or zero, but only outputs with positive values
+ are created. There are no type restrictions, but the value type needs to
+ support support addition, subtraction, and comparison.
+
+ * The :meth:`elements` method requires integer counts. It ignores zero and
+ negative counts.
+
.. seealso::
* `Counter class <http://code.activestate.com/recipes/576611/>`_