[Python-ideas] collections.Counter multiplication

James K jamylak at gmail.com
Wed May 29 22:17:34 CEST 2013


It should work like this

    >>> from collections import Counter
    >>> Counter({'a': 1, 'b': 2}) * 2 # scalar
    Counter({'b': 4, 'a': 2})
    >>> Counter({'a': 1, 'b': 2}) * Counter({'c': 1, 'b': 2}) # multiplies
matching keys
    Counter({'b': 4})


This is intuitive behavior and therefore should be added. I am unsure about
division as dividing by a non-existing key would be a division by 0,
although division by a scalar is straightforward.

On Thu, May 30, 2013 at 12:29 AM, Ned Batchelder <ned at nedbatchelder.com>wrote:

>  On 5/29/2013 1:27 AM, James K wrote:
>
> Can we add a multiplication feature to collections.Counter, I don't see
> why not.
>
>
> James, welcome to the list.  To get an idea accepted, you have to do a few
> things:
>
> 1) Explain the idea fully.  I don't understand what "a multiplication
> feature" would do.
> 2) Explain why the idea is useful to enough people that it should be added
> to the standard library.
>
> These two criteria are not easy to meet.  Sometimes an idea seems popular,
> but it turns out that different people want it to behave differently, or
> differently at different times (see the discussion about an
> itertools.chunked feature).  Sometimes an idea is straightforward enough to
> describe, but is useful to too few people to justify adding it to the
> standard library.
>
> Discussing these things doesn't often result in a change to Python, but
> does often lead to useful discussion.
>
> --Ned.
>
>
>
> _______________________________________________
> Python-ideas mailing listPython-ideas at python.orghttp://mail.python.org/mailman/listinfo/python-ideas
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130530/00202ef2/attachment.html>


More information about the Python-ideas mailing list