[issue45936] collections.Counter drops key if value is 0 and updating using += operator

2021-11-29 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
assignee:  -> rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45936] collections.Counter drops key if value is 0 and updating using += operator

2021-11-29 Thread Raymond Hettinger


Raymond Hettinger  added the comment:

I don't think there is a need to list the inplace methods.  They were put in to 
optimize what was already occurring when only the __add__ method was defined.  
Also, other container typically don't specifically call out the inplace methods.

--
nosy: +rhettinger
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45936] collections.Counter drops key if value is 0 and updating using += operator

2021-11-29 Thread Dennis Sweeney


Dennis Sweeney  added the comment:

This is consistent with the docstrings of the methods:

-
>>> help(Counter.__iadd__)
Help on function __iadd__ in module collections:

__iadd__(self, other)
Inplace add from another counter, keeping only positive counts.

>>> c = Counter('abbb')
>>> c += Counter('bcc')
>>> c
Counter({'b': 4, 'c': 2, 'a': 1})

>>> help(Counter.update)
Help on function update in module collections:

update(self, iterable=None, /, **kwds)
Like dict.update() but add counts instead of replacing them.

Source can be an iterable, a dictionary, or another Counter instance.

>>> c = Counter('which')
>>> c.update('witch')   # add elements from another iterable
>>> d = Counter('watch')
>>> c.update(d) # add elements from another counter
>>> c['h']  # four 'h' in which, witch, and watch
4
-


However, it seems Counter.__iadd__ is not mentioned at all at 
https://docs.python.org/3/library/collections.html#collections.Counter. I think 
there could be a doc update.

--
nosy: +Dennis Sweeney

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45936] collections.Counter drops key if value is 0 and updating using += operator

2021-11-29 Thread Richard Decal


New submission from Richard Decal :

In brief:

```
from collections import Counter
x = Counter({'a': 0, 'b': 1})
x.update(x)  # works: Counter({'a': 0, 'b': 2})
x += x  # expected: Counter({'a': 0, 'b': 3}) actual: Counter({'b': 3})
```

I expect `+=` and `.update()` to be synonymous. However, the += operator is 
deleting keys if the source Counter has a zero count to begin with:

```
x = Counter({'a': 1})
x += Counter({'a': 0})  # ok: Counter({'a': 1})

y = Counter({'a': 0})
y += y  # expected: Counter({'a': 0}) actual: Counter()
```

--
messages: 407348
nosy: crypdick
priority: normal
severity: normal
status: open
title: collections.Counter drops key if value is 0 and updating using += 
operator
type: behavior
versions: Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com