[Python-ideas] collections.Counter multiplication

MRAB python at mrabarnett.plus.com
Thu May 30 00:47:40 CEST 2013


On 29/05/2013 21:17, James K 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.
>
Multiplying by scalars I understand, but by another Counter? That just
feels wrong to me.

For example:

 >>> c = Counter("apple": 3, "orange": 5)
 >>> # Double everything.
 >>> c * 2
Counter("apple": 6, "orange": 10)

Fine, OK.

But what does _this_ mean?

 >>> d = Counter("orange": 4, "pear": 2)
 >>> c * d
???



More information about the Python-ideas mailing list