[Python-ideas] collections.Counter should implement __mul__, __rmul__

Peter Norvig peter at norvig.com
Sun Apr 15 19:02:50 EDT 2018


That's actually how I coded it myself the first time. But I worried it
would be wasteful to create an intermediate dict and discard it.

`timeit` results:

3.79 µs for the for-loop, 5.08 µs for the dict-comprehension with a 10-key
Counter
257 µs for the for-loop, 169 µs for the dict-comprehension with a 1000-key
Counter

So results are mixed, but you are probably right.

On Sun, Apr 15, 2018 at 3:46 PM Wes Turner <wes.turner at gmail.com> wrote:

> Good call. Is it any faster to initialize Counter with a dict
> comprehension?
>
> return Counter({k: v*scalar for (k, v) in self.items())
>
> On Sun, Apr 15, 2018 at 5:05 PM, Peter Norvig <peter at norvig.com> wrote:
>
>> For most types that implement __add__, `x + x` is equal to `2 * x`.
>>
>> That is true for all numbers, list, tuple, str, timedelta, etc. -- but
>> not for collections.Counter. I can add two Counters, but I can't multiply
>> one by a scalar. That seems like an oversight.
>>
>> It would be worthwhile to implement multiplication because, among other
>> reasons, Counters are a nice representation for discrete probability
>> distributions, for which multiplication is an even more fundamental
>> operation than addition.
>>
>> Here's an implementation:
>>
>>     def __mul__(self, scalar):
>>         "Multiply each entry by a scalar."
>>         result = Counter()
>>         for key in self:
>>             result[key] = self[key] * scalar
>>         return result
>>
>>     def __rmul__(self, scalar):
>>         "Multiply each entry by a scalar."
>>         result = Counter()
>>         for key in self:
>>             result[key] = scalar * self[key]
>>         return result
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180415/18caf884/attachment.html>


More information about the Python-ideas mailing list