简体   繁体   English

collections.Counter 上的最大值

[英]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. collections.Counter上的最大值是反直觉的,我想找到字符串中出现次数最多的字符。

>>> 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.我知道我应该使用most_common ,但它的使用似乎是做作的。

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

Is there a case for supporting max on Counter?是否有在 Counter 上支持 max 的情况?

You could use the key parameter of max :您可以使用maxkey参数:

max(c, key=c.get)

output: 'a' 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). Counter.most_common执行排序,因此以这种方式使用max也应该更快(快速测试告诉我这是小型计数器的情况,而大型计数器的差异有限)。

max with key seems to be faster than most_common带键的 max 似乎比 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]
>>> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM