Home > Blockchain >  max on collections.Counter
max on collections.Counter

Time:11-24

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 ?

CodePudding user response:

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).

CodePudding user response:

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]
>>> 
  • Related