collections Counter most_common method
Roy Smith
roy at panix.com
Sat Dec 14 14:37:15 EST 2013
In article <mailman.4121.1387047279.18130.python-list at python.org>,
Mark Lawrence <breamoreboy at yahoo.co.uk> 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.
The reason to return iterators instead of lists is that it's more
efficient if the list is very long. I would imagine the most common use
cases for most_common() are to pass it a number like 3. You typically
want to find the most common, or the three most common, or maybe the 10
most common. It's rare that people want the 5000 most common.
So, while I suppose returning an iterator might be more efficient in
some cases, those cases seem pretty rare.
More information about the Python-list
mailing list