[Python-ideas] collections.Counter multiplication

David Mertz mertz at gnosis.cx
Wed May 29 23:08:05 CEST 2013


I don't see the use case here.  I guess the described behavior is mildly interesting, but it doesn't feel widely useful.  For your own need, why not just write:

class MyCounter(Counter):
    def __mul__(self, x):
        new = Counter()
        if isinstance(x, int):
            for k in self.keys():
                new[k] = x * self[k]
            return new
        elif isinstance(x, Counter):
            for k in self.keys():
                if x[k]:
                    new[k] = x[k] * self[k]
            return new
        else:
            raise ValueError("Can only multiply MyCounter by int or Counter")


On May 29, 2013, at 1:42 PM, James K wrote:
> Oops I forgot Counter doesn't support operations with scalars, I'm still proposing the second example
> 
> 
> On Thu, May 30, 2013 at 6:21 AM, David Mertz <mertz at gnosis.cx> wrote:
> That "intuitive" behavior certainly wouldn't have been my first--or second guess--on seeing the syntax.
> 
> 
> On Wed, May 29, 2013 at 1:17 PM, 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
> 
> 
> 
> 
> -- 
> Keeping medicines from the bloodstreams of the sick; food 
> from the bellies of the hungry; books from the hands of the 
> uneducated; technology from the underdeveloped; and putting 
> advocates of freedom in prisons.  Intellectual property is
> to the 21st century what the slave trade was to the 16th.
> 

--
mertz@ | The specter of free information is haunting the `Net!  All the
gnosis | powers of IP- and crypto-tyranny have entered into an unholy
.cx    | alliance...ideas have nothing to lose but their chains.  Unite
       | against "intellectual property" and anti-privacy regimes!



More information about the Python-ideas mailing list