summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2011-01-03 08:50:48 (GMT)
committerRaymond Hettinger <python@rcn.com>2011-01-03 08:50:48 (GMT)
commit8dfb9288af2ce4118861969bd2a6666cb736f5d5 (patch)
tree402e28b3202b5b3b3b577ee2e2bb9051f2b5c3fd /Lib
parent32e8aab1fbb809f3e4bd0830f0b0602349f21a02 (diff)
downloadcpython-8dfb9288af2ce4118861969bd2a6666cb736f5d5.zip
cpython-8dfb9288af2ce4118861969bd2a6666cb736f5d5.tar.gz
cpython-8dfb9288af2ce4118861969bd2a6666cb736f5d5.tar.bz2
Backport r87672 and r87615, improving tests, using super() instead of direct parent references, and using __reduce__ method for pickling.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/collections.py26
1 files changed, 15 insertions, 11 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index 4ac50d8..3329f08 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -298,16 +298,16 @@ class Counter(dict):
or multiset. Elements are stored as dictionary keys and their counts
are stored as dictionary values.
- >>> c = Counter('abracadabra') # count elements from a string
+ >>> c = Counter('abcdeabcdabcaba') # count elements from a string
>>> c.most_common(3) # three most common elements
- [('a', 5), ('r', 2), ('b', 2)]
+ [('a', 5), ('b', 4), ('c', 3)]
>>> sorted(c) # list all unique elements
- ['a', 'b', 'c', 'd', 'r']
+ ['a', 'b', 'c', 'd', 'e']
>>> ''.join(sorted(c.elements())) # list elements with repetitions
- 'aaaaabbcdrr'
+ 'aaaaabbbbcccdde'
>>> sum(c.values()) # total of all counts
- 11
+ 15
>>> c['a'] # count of letter 'a'
5
@@ -315,8 +315,8 @@ class Counter(dict):
... c[elem] += 1 # by adding 1 to each element's count
>>> c['a'] # now there are seven 'a'
7
- >>> del c['r'] # remove all 'r'
- >>> c['r'] # now there are zero 'r'
+ >>> del c['b'] # remove all 'b'
+ >>> c['b'] # now there are zero 'b'
0
>>> d = Counter('simsalabim') # make another counter
@@ -355,6 +355,7 @@ class Counter(dict):
>>> c = Counter(a=4, b=2) # a new counter from keyword args
'''
+ super().__init__()
self.update(iterable, **kwds)
def __missing__(self, key):
@@ -366,8 +367,8 @@ class Counter(dict):
'''List the n most common elements and their counts from the most
common to the least. If n is None, then list all element counts.
- >>> Counter('abracadabra').most_common(3)
- [('a', 5), ('r', 2), ('b', 2)]
+ >>> Counter('abcdeabcdabcaba').most_common(3)
+ [('a', 5), ('b', 4), ('c', 3)]
'''
# Emulate Bag.sortedByCount from Smalltalk
@@ -433,7 +434,7 @@ class Counter(dict):
for elem, count in iterable.items():
self[elem] = count + self_get(elem, 0)
else:
- dict.update(self, iterable) # fast path when counter is empty
+ super().update(iterable) # fast path when counter is empty
else:
self_get = self.get
for elem in iterable:
@@ -445,10 +446,13 @@ class Counter(dict):
'Like dict.copy() but returns a Counter instance instead of a dict.'
return Counter(self)
+ def __reduce__(self):
+ return self.__class__, (dict(self),)
+
def __delitem__(self, elem):
'Like dict.__delitem__() but does not raise KeyError for missing values.'
if elem in self:
- dict.__delitem__(self, elem)
+ super().__delitem__(elem)
def __repr__(self):
if not self: