Skip to content

Commit daeab96

Browse files
committed
Implement n=1 for .most_common
1 parent cd52172 commit daeab96

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

Lib/collections/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,11 @@ def most_common(self, n=None):
634634
if n is None:
635635
return sorted(self.items(), key=_itemgetter(1), reverse=True)
636636

637+
if n == 1:
638+
if not self.items():
639+
return []
640+
return [max(self.items(), key=_itemgetter(1))]
641+
637642
return _nlargest(n, self.items(), key=_itemgetter(1))
638643

639644
def elements(self):

Lib/test/test_collections.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,6 +2134,67 @@ def test_basics(self):
21342134
self.assertEqual(c['d'], 1)
21352135
self.assertEqual(c.setdefault('e', 5), 5)
21362136
self.assertEqual(c['e'], 5)
2137+
2138+
def test_most_common_n_equals_one(self):
2139+
# Test n == 1 returns a list with one tuple
2140+
c1 = Counter('abcaba')
2141+
result = c1.most_common(1)
2142+
self.assertEqual(result, [('a', 3)])
2143+
self.assertIsInstance(result, list)
2144+
self.assertEqual(len(result), 1)
2145+
self.assertIsInstance(result[0], tuple)
2146+
2147+
# Test n == 1 returns same format as other n values
2148+
self.assertEqual(c1.most_common(1), c1.most_common(2)[:1])
2149+
2150+
# Test with a counter where all items have same count
2151+
c2 = Counter('abc')
2152+
result = c2.most_common(1)
2153+
self.assertEqual(len(result), 1)
2154+
self.assertIn(result[0][1], [1]) # count should be 1
2155+
2156+
# Test n == 1 with single element
2157+
c3 = Counter('a')
2158+
self.assertEqual(c3.most_common(1), [('a', 1)])
2159+
2160+
# Test n == 1 with empty counter
2161+
c4 = Counter()
2162+
self.assertEqual(c4.most_common(1), [])
2163+
2164+
# Test that n == 1 and n == 2 are consistent at index 0
2165+
c5 = Counter('the quick brown fox')
2166+
one = c5.most_common(1)
2167+
two = c5.most_common(2)
2168+
self.assertEqual(one[0], two[0])
2169+
2170+
# Test n == 1 with negative counts
2171+
c6 = Counter({'a': -3, 'b': -1, 'c': 5})
2172+
self.assertEqual(c6.most_common(1), [('c', 5)])
2173+
2174+
# Test n == 1 with zero counts mixed in
2175+
c7 = Counter({'x': 0, 'y': 0, 'z': 1})
2176+
self.assertEqual(c7.most_common(1), [('z', 1)])
2177+
2178+
# Test n == 1 consistency with most_common(None)[0]
2179+
c8 = Counter('abracadabra')
2180+
self.assertEqual(c8.most_common(1)[0], c8.most_common()[0])
2181+
2182+
# Test n == 1 with counter built from keyword args
2183+
c9 = Counter(cat=10, dog=3, bird=7)
2184+
self.assertEqual(c9.most_common(1), [('cat', 10)])
2185+
2186+
# Test n == 1 with counter built from mapping
2187+
c10 = Counter({'apple': 50, 'banana': 20, 'cherry': 30})
2188+
self.assertEqual(c10.most_common(1), [('apple', 50)])
2189+
2190+
# Test n == 1 with large number of unique keys
2191+
c11 = Counter({i: i for i in range(1000)})
2192+
self.assertEqual(c11.most_common(1), [(999, 999)])
2193+
2194+
# Test n == 1 with a Counter subclass
2195+
c12 = CounterSubclassWithSetItem('aabbbc')
2196+
result = c12.most_common(1)
2197+
self.assertEqual(result, [('b', 3)])
21372198

21382199
def test_update_reentrant_add_clears_counter(self):
21392200
c = Counter()

0 commit comments

Comments
 (0)