MCPcopy Index your code
hub / github.com/python/cpython / most_common

Method most_common

Lib/collections/__init__.py:625–637  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.

Callers 5

__repr__Method · 0.95
test_basicsMethod · 0.95
modeFunction · 0.80
_pprint_counterMethod · 0.80

Calls 1

itemsMethod · 0.45

Tested by 1

test_basicsMethod · 0.76