[Python-ideas] Implementing a set of operation (+, /, - *) on dict consistent with linearAlgebrae

David Mertz mertz at gnosis.cx
Tue Oct 30 19:22:45 EDT 2018


Actually, they are definitely different as in-place mutation versus
returning a new Counter. But in some arithmetic way they look mostly the
same.

On Tue, Oct 30, 2018, 7:19 PM David Mertz <mertz at gnosis.cx wrote:

> Counter doesn't QUITE do the same thing as this `mdict`.  But it's pretty
> close.
>
> I think if .__add__() became a synonym for .update() that wouldn't break
> anything that currently works.  But I'm probably wrong, and missing a case
> in my quick thought:
>
> >>> from collections import Counter
> >>> c = Counter(a=[2], b='a')
> >>> c.update(c)
> >>> c
> Counter({'a': [2, 2], 'b': 'aa'})
> >>> c2 = Counter(a=1, b=2)
> >>> c2 + c2
> Counter({'b': 4, 'a': 2})
> >>> c2.update(c2)
> >>> c2
> Counter({'b': 4, 'a': 2})
> >>> c + c
> Traceback (most recent call last):
>   File "<ipython-input-18-e88785f3c342>", line 1, in <module>
>     c + c
>   File "/anaconda3/lib/python3.6/collections/__init__.py", line 705, in
> __add__
>     if newcount > 0:
> TypeError: '>' not supported between instances of 'list' and 'int'
>
> On Tue, Oct 30, 2018 at 6:54 PM Alexander Belopolsky <
> alexander.belopolsky at gmail.com> wrote:
>
>> > In [12]: a= mdict(a=[2], b='a')
>> > In [13]: a+a
>>
>> Aren't you reinventing the Counter type?
>>
>> >>> from collections import Counter
>> >>> c = Counter(a=1,b=2)
>> >>> c + c
>> Counter({'b': 4, 'a': 2})
>> _______________________________________________
>> 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/
>>
>
>
> --
> 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.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20181030/ea6e1226/attachment.html>


More information about the Python-ideas mailing list