Most frequently occuring n words in a string
<p>I have a problem with the following problem:</p>

<p><strong>Problem</strong>:</p>

<p>Implement a function count_words() in Python that takes as input a string s and a number n, and returns the n most frequently-occuring words in s. The return value should be a list of tuples - the top n words paired with their respective counts [(, ), (, ), ...], sorted in descending count order.</p>

<p>You can assume that all input will be in lowercase and that there will be no punctuations or other characters (only letters and single separating spaces). In case of a tie (equal count), order the tied words alphabetically.</p>

<p>E.g.:</p>

<p>print count_words("betty bought a bit of butter but the butter was bitter",3)
Output:</p>

<p>[('butter', 2), ('a', 1), ('betty', 1)]</p>

<p><strong>This is my solution:</strong></p>

<pre><code>    """Count words."""

from operator import itemgetter
from collections import Counter

def count_words(s, n):
    """Return the n most frequently occuring words in s."""

    # TODO: Count the number of occurences of each word in s
    words = s.split(" ");
    words = Counter(words)
    # TODO: Sort the occurences in descending order (alphabetically in case of ties)
    print(words)
    # TODO: Return the top n words as a list of tuples (&lt;word&gt;, &lt;count&gt;)
    top_n = words.most_common(n)
    return top_n

def test_run()

    """Test count_words() with some inputs."""
    print(count_words("cat bat mat cat bat cat", 3))
    print(count_words("betty bought a bit of butter but the butter was bitter", 3))


if __name__ == '__main__':
    test_run()
</code></pre>

<p>The problem is that elements with equal counts are ordered arbitrarily, how can i order that elements by alphabetical order ??</p>

<p>You can sort them using the <em>number of occurrence</em> (in reverse order) and then the <em>lexicographical order</em>:</p>

<pre><code>&gt;&gt;&gt; lst = [('meat', 2), ('butter', 2), ('a', 1), ('betty', 1)]
&gt;&gt;&gt; 
&gt;&gt;&gt; sorted(lst, key=lambda x: (-x[1], x[0]))
#                              ^ reverse order 
[('butter', 2), ('meat', 2), ('a', 1), ('betty', 1)]
</code></pre>

<p>The number of occurrence takes precedence over the lex. order.</p>

<p>In your case, use <code>words.items()</code> in place of the list of the list I have used. You will no longer need to use <code>most_common</code> as <code>sorted</code> already does the same.</p>

python