collections Counter most_common method

Mark Lawrence breamoreboy at yahoo.co.uk
Sun Dec 15 17:36:07 CET 2013


On 14/12/2013 19:42, Peter Otten wrote:
> Mark Lawrence wrote:
>
>> This method returns a list, the example from The Fine Docs being:-
>>
>>   >>> Counter('abracadabra').most_common(3)
>> [('a', 5), ('r', 2), ('b', 2)]
>>
>> With the trend in Python being more and more towards methods returning
>> iterators, is there ever likely to be an imost_common method, or has
>> this been suggested and rejected, or what?  I'm really just curious, but
>> if enough people were to express an interest and it hasn't already been
>> done, I'd happily raise an enhancement request on the bug tracker.
>
> As Counter is currently a dict you have to look at all items to find the
> most common. With that underlying data structure I don't see how it could
> make sense to yield the most common items incrementally. So
>
> (1) When would you prefer it over the the eager variant?
> (2) How would you implement it?
>
> By the way, if you can come up with a plausible answer for the second
> question you should aim higher, for lazy heapq.nlargest() and sorted()
> functions...
>

Coming from my No. 1 MVP (Most Valuable Pythonista) I'll take that as an 
emphatic no :)

The good news from my POV is that while thinking about this, then 
looking at my code, I completely eliminated an unneeded data structure.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence




More information about the Python-list mailing list