collections Counter most_common method
Peter Otten
__peter__ at web.de
Sat Dec 14 14:42:27 EST 2013
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...
More information about the Python-list
mailing list