Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fcb393c

Browse files
committed
Add support for unary plus and unary minus to collections.Counter()
1 parent 18205ba commit fcb393c

4 files changed

Lines changed: 31 additions & 1 deletion

File tree

Doc/library/collections.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ Common patterns for working with :class:`Counter` objects::
264264
c.items() # convert to a list of (elem, cnt) pairs
265265
Counter(dict(list_of_pairs)) # convert from a list of (elem, cnt) pairs
266266
c.most_common()[:-n:-1] # n least common elements
267-
c += Counter() # remove zero and negative counts
267+
+c # remove zero and negative counts
268268

269269
Several mathematical operations are provided for combining :class:`Counter`
270270
objects to produce multisets (counters that have counts greater than zero).
@@ -284,6 +284,18 @@ counts, but the output will exclude results with counts of zero or less.
284284
>>> c | d # union: max(c[x], d[x])
285285
Counter({'a': 3, 'b': 2})
286286

287+
Unary addition and substraction are shortcuts for adding an empty counter
288+
or subtracting from an empty counter.
289+
290+
>>> c = Counter(a=2, b=-4)
291+
>>> +c
292+
Counter({'a': 2})
293+
>>> -c
294+
Counter({'b': 4})
295+
296+
.. versionadded:: 3.3
297+
Added support for unary plus and unary minus.
298+
287299
.. note::
288300

289301
Counters were primarily designed to work with positive integers to represent

Lib/collections/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,17 @@ def __and__(self, other):
672672
result[elem] = newcount
673673
return result
674674

675+
def __pos__(self):
676+
'Adds an empty counter, effectively stripping negative and zero counts'
677+
return self + Counter()
678+
679+
def __neg__(self):
680+
'''Subtracts from an empty counter. Strips positive and zero counts,
681+
and flips the sign on negative counts.
682+
683+
'''
684+
return Counter() - self
685+
675686

676687
########################################################################
677688
### ChainMap (helper for configparser and string.Template)

Lib/test/test_collections.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,11 @@ def test_subtract(self):
943943
c.subtract('aaaabbcce')
944944
self.assertEqual(c, Counter(a=-1, b=0, c=-1, d=1, e=-1))
945945

946+
def test_unary(self):
947+
c = Counter(a=-5, b=0, c=5, d=10, e=15,g=40)
948+
self.assertEqual(dict(+c), dict(c=5, d=10, e=15, g=40))
949+
self.assertEqual(dict(-c), dict(a=5))
950+
946951
def test_helper_function(self):
947952
# two paths, one for real dicts and one for other mappings
948953
elems = list('abracadabra')

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ Library
252252
- Issue #12540: Prevent zombie IDLE processes on Windows due to changes
253253
in os.kill().
254254

255+
- Add support for unary plus and unary minus to collections.Counter().
256+
255257
- Issue #12683: urlparse updated to include svn as schemes that uses relative
256258
paths. (svn from 1.5 onwards support relative path).
257259

0 commit comments

Comments
 (0)