mirror of
https://github.com/python/cpython.git
synced 2025-10-22 22:53:06 +00:00
Fix doctest to not rely on order of dictionary entries.
Use super() instead of direct references to the dict superclass.
This commit is contained in:
parent
345c49b16b
commit
00d43fd056
1 changed files with 12 additions and 11 deletions
|
@ -366,16 +366,16 @@ class Counter(dict):
|
||||||
or multiset. Elements are stored as dictionary keys and their counts
|
or multiset. Elements are stored as dictionary keys and their counts
|
||||||
are stored as dictionary values.
|
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
|
>>> 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
|
>>> sorted(c) # list all unique elements
|
||||||
['a', 'b', 'c', 'd', 'r']
|
['a', 'b', 'c', 'd', 'e']
|
||||||
>>> ''.join(sorted(c.elements())) # list elements with repetitions
|
>>> ''.join(sorted(c.elements())) # list elements with repetitions
|
||||||
'aaaaabbcdrr'
|
'aaaaabbbbcccdde'
|
||||||
>>> sum(c.values()) # total of all counts
|
>>> sum(c.values()) # total of all counts
|
||||||
11
|
15
|
||||||
|
|
||||||
>>> c['a'] # count of letter 'a'
|
>>> c['a'] # count of letter 'a'
|
||||||
5
|
5
|
||||||
|
@ -383,8 +383,8 @@ class Counter(dict):
|
||||||
... c[elem] += 1 # by adding 1 to each element's count
|
... c[elem] += 1 # by adding 1 to each element's count
|
||||||
>>> c['a'] # now there are seven 'a'
|
>>> c['a'] # now there are seven 'a'
|
||||||
7
|
7
|
||||||
>>> del c['r'] # remove all 'r'
|
>>> del c['b'] # remove all 'b'
|
||||||
>>> c['r'] # now there are zero 'r'
|
>>> c['b'] # now there are zero 'b'
|
||||||
0
|
0
|
||||||
|
|
||||||
>>> d = Counter('simsalabim') # make another counter
|
>>> d = Counter('simsalabim') # make another counter
|
||||||
|
@ -423,6 +423,7 @@ class Counter(dict):
|
||||||
>>> c = Counter(a=4, b=2) # a new counter from keyword args
|
>>> c = Counter(a=4, b=2) # a new counter from keyword args
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
super().__init__()
|
||||||
self.update(iterable, **kwds)
|
self.update(iterable, **kwds)
|
||||||
|
|
||||||
def __missing__(self, key):
|
def __missing__(self, key):
|
||||||
|
@ -434,8 +435,8 @@ class Counter(dict):
|
||||||
'''List the n most common elements and their counts from the most
|
'''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.
|
common to the least. If n is None, then list all element counts.
|
||||||
|
|
||||||
>>> Counter('abracadabra').most_common(3)
|
>>> Counter('abcdeabcdabcaba').most_common(3)
|
||||||
[('a', 5), ('r', 2), ('b', 2)]
|
[('a', 5), ('b', 4), ('c', 3)]
|
||||||
|
|
||||||
'''
|
'''
|
||||||
# Emulate Bag.sortedByCount from Smalltalk
|
# Emulate Bag.sortedByCount from Smalltalk
|
||||||
|
@ -501,7 +502,7 @@ class Counter(dict):
|
||||||
for elem, count in iterable.items():
|
for elem, count in iterable.items():
|
||||||
self[elem] = count + self_get(elem, 0)
|
self[elem] = count + self_get(elem, 0)
|
||||||
else:
|
else:
|
||||||
dict.update(self, iterable) # fast path when counter is empty
|
super().update(iterable) # fast path when counter is empty
|
||||||
else:
|
else:
|
||||||
_count_elements(self, iterable)
|
_count_elements(self, iterable)
|
||||||
if kwds:
|
if kwds:
|
||||||
|
@ -541,7 +542,7 @@ class Counter(dict):
|
||||||
def __delitem__(self, elem):
|
def __delitem__(self, elem):
|
||||||
'Like dict.__delitem__() but does not raise KeyError for missing values.'
|
'Like dict.__delitem__() but does not raise KeyError for missing values.'
|
||||||
if elem in self:
|
if elem in self:
|
||||||
dict.__delitem__(self, elem)
|
super().__delitem__(elem)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
if not self:
|
if not self:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue