bpo-25478: Add total() method to collections.Counter (GH-25829)

This commit is contained in:
Raymond Hettinger 2021-05-02 20:19:51 -07:00 committed by GitHub
parent d52bbde942
commit 8c598dbb94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 1 deletions

View file

@ -581,6 +581,10 @@ class Counter(dict):
# Needed so that self[missing_item] does not raise KeyError
return 0
def total(self):
'Sum of the counts'
return sum(self.values())
def most_common(self, n=None):
'''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.