max on collections.Counter

The max on collections.Counter is counter intuitive, I want to find the find the character that occurs the most in a string.

>>> from collections import Counter
>>> c = Counter('aaaabbbcc')
>>> max(c)
'c'
>>> c
Counter({'a': 4, 'b': 3, 'c': 2})

I know I should be using most_common, but its use seems contrived.

>>> c.most_common(1)[0][0]
'a'

Is there a case for supporting max on Counter ?

9

2 Answers

You could use the key parameter of max:

max(c, key=c.get)

output: 'a'

NB. Counter.most_common performs sorting, so using max this way should also be faster (a quick test tells me this is the case on small Counters while there is limited difference on large Counters).

2

max with key seems to be faster than most_common

>>> from collections import Counter
>>> import timeit
>>> s0 = 'aaaabbbcc'
>>> s1 = s0[:] * 100
>>> def f_max(s): return max((c := Counter(s)), key=c.get)
>>> def f_common(s): return Counter(s).most_common(1)[0][0]
>>> timeit.repeat("f_max(s1)", "from __main__ import f_max, f_common, s1", number=10000)
[0.32935670800000594, 0.32097511900002473, 0.3285609399999885, 0.3300831690000052, 0.326068628999991]
>>> timeit.repeat("f_common(s1)", "from __main__ import f_max, f_common, s1", number=10000)
[0.3436732490000054, 0.3355550489999928, 0.34284031400000003, 0.343095218000002, 0.34329394300002036]
>>> 
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like