[Python-ideas] collections.Counter multiplication

MRAB python at mrabarnett.plus.com
Thu May 30 17:18:25 CEST 2013


On 30/05/2013 15:50, Steven D'Aprano wrote:
> On 30/05/13 21:10, MRAB wrote:
>> On 30/05/2013 02:29, Steven D'Aprano wrote:
>>> On 30/05/13 10:52, Matthew Ruffalo wrote:
>>>> On 05/29/2013 08:06 PM, Steven D'Aprano wrote:
>>>>> On 30/05/13 06: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})
>>>>>
>>>>> Under what circumstances would you do this?
>>>>>
>>>>> What is it about Counters that they should support multiplication when no other mapping type does?
>>>>
>>>> Counters are different from other mapping types because they provide a natural Python stdlib implementation of multisets -- the docs explicitly state that "The Counter class is similar to bags or multisets in other languages.". The class already has behavior that is different from other mapping types: Counter.__init__ can also take an iterable of hashable objects instead of another mapping, and Counter.update adds counts instead of replacing them.
>>>
>>>
>>> None of this answers my question. Under what circumstances would you multiply a counter by a scalar (let alone by another counter)? The fact that counters differ in some ways from other mappings doesn't justify every arbitrary change proposed.
>>>
>> Well, you can add Counters together:
>>
>>>>> from collections import Counter
>>>>> Counter({'a': 1, 'b': 2}) + Counter({'a': 1, 'b': 2})
>> Counter({'b': 4, 'a': 2})
>>
>> so multiplying by a non-negative scalar would have the same behaviour.
>
> With respect MRAB, you are also not answering my question, which is about *why* somebody might wish to multiply a Counter, not what it would do. I already know that multiplication by an integer is equivalent to repeated addition. What I'm asking for is a use-case, for why one might find this functionality useful, not on what the functionality is.
>
> For example, the use-case for Counter addition might be:
>
> "I have used a Counter to count items from one data set, and another Counter to count items from a different data set. Now I want to count items from both data sets together. Rather than concatenate the two data sets (which may not even be possible, if they were from iterators) and re-count, I can simply add the two Counters."
>
> That's a great use-case, and it justifies supporting Counter + Counter.
>
> The use-case for Counter multiplication escapes me, unless it is this:
>
> "I have a Counter. I want to multiply all the counts by a constant, just because I can."
>
"I have a batch of identical collections of items. How many do I have
of each item?"
>
> To the Original Poster, James K:
>
> If the only justification for this proposal is that you think it is logical that Counters should support this behaviour, then I am against it. Adding this behaviour requires more code, more tests, more documentation, more things for users to learn. But if you have a good use-case showing some circumstances where a programmer would find it helpful to have this functionality, then please tell us.
>



More information about the Python-ideas mailing list