[Python-ideas] collections.Counter multiplication

Andrew Barnert abarnert at yahoo.com
Thu May 30 03:47:10 CEST 2013


Given how simple this would be to implement, I think the obvious way forward is to write an implementation, put it on PyPI, and give it time to see if it gets any traction. If enough people use it, it can be easily added to the stdlib later.

(IIRC, there's a project named something like more-collections that has OrderedSet, OrderedDefaultDict, etc., so you could submit this new class as a patch to that project instead of creating a new one, but that doesn't make too much difference.)

If you don't know how to write the implementation yourself, just ask and someone will write it for you.

Sent from a random iPhone

On May 29, 2013, at 13:17, James K <jamylak at gmail.com> wrote:

> 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 list
>>> Python-ideas at python.org
>>> http://mail.python.org/mailman/listinfo/python-ideas
> 
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130529/5982fd66/attachment.html>


More information about the Python-ideas mailing list