[Python-ideas] Support floating-point values in collections.Counter
Franklin? Lee
leewangzhong+python at gmail.com
Wed Dec 20 16:49:09 EST 2017
On Mon, Dec 18, 2017 at 6:51 PM, Joel Croteau <jcroteau at gmail.com> wrote:
> It would be useful in many scenarios for values in collections.Counter to be
> allowed to be floating point. I know that Counter nominally emulates a
> multiset, which would suggest only integer values, but in a more general
> sense, it could be an accumulator of either floating point or integer data.
> As near as I can tell, Collection already does support float values in both
> Python 2.7 and 3.6, and the way the code is implemented, this change should
> be a no-op. All that is required is to update the documentation to say
> floating-point values are allowed, as it currently says only integers are
> allowed.
That's beyond the scope of Counter. I think what you really want is a
generalization of Counter which represents a key'd number bag. A dict
of key=>number which supports arithmetic operations, like Numpy arrays
are to lists.
Example methods:
__init__(...): Like dict's version, but it will combine the values
of duplicate keys in its params.
update(...): Similar to __init__.
fromkeys(...): Like dict's version, but uses 0 or 1 as the default
value, and combines values like the constructor. With value=1, this is
roughly equivalent to the Counter constructor.
<arithmetic operators>: Arithmetic with other number bags, with
dicts, and with number-like values.
clearsmall(tolerance=0): Removes keys whose values are close to 0.
Other methods may take inspiration from Numpy.
The class should probably be a third-party package (and probably
already is), so that the method list can be solidified first.
More information about the Python-ideas
mailing list