[Tutor] Tutor Digest, Vol 166, Issue 21

Alan Gauld alan.gauld at yahoo.co.uk
Mon Dec 25 04:37:26 EST 2017


On 25/12/17 03:46, anish singh wrote:
>>> However, I am stuck. I have below code which is not working.
> 
> I don't know how to achieve this programmatically: sorted by the
> number of occurrences in a descending order. If two or more words
> have the same count, they should be sorted
> alphabetically (in an ascending order).

There is probably  way to write a sort function that will
do what I want but personally I'd divide and conquer by
putting the results into two chunks. First I'd have a new
dictionary keyed by the frequency. And the values in each
frequency would be lists sorted alphabetically. So, for
your data, it would look like:

data = {
         4:['just'],
         3:['practice'],
         2:['perfect'],
         1:['by','get','makes','you\'ll']
        }

> #expected: [["just","4"],["practice","3"],["perfect","2"],["makes","1"],["youll","1"],["get","1"],["by","1"]]

Really? I'd expect:
[["just","4"],["practice","3"],["perfect","2"],["by","1"],["get","1"],["makes","1"],["you'll","1"]]

as I did in the dict above.

HTH

Alan G.



More information about the Tutor mailing list