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), ('b', 2), ('r', 2)]
(self, n=None)
| 623 | return sum(self.values()) |
| 624 | |
| 625 | def most_common(self, n=None): |
| 626 | '''List the n most common elements and their counts from the most |
| 627 | common to the least. If n is None, then list all element counts. |
| 628 | |
| 629 | >>> Counter('abracadabra').most_common(3) |
| 630 | [('a', 5), ('b', 2), ('r', 2)] |
| 631 | |
| 632 | ''' |
| 633 | # Emulate Bag.sortedByCount from Smalltalk |
| 634 | if n is None: |
| 635 | return sorted(self.items(), key=_itemgetter(1), reverse=True) |
| 636 | |
| 637 | return _nlargest(n, self.items(), key=_itemgetter(1)) |
| 638 | |
| 639 | def elements(self): |
| 640 | '''Iterator over elements repeating each as many times as its count. |