Skip to content
Advertisement

Sorting by word count that store in dict python

how can you sort by word count? sorted() sort me only by the number of numbers? Thank you for your help

JavaScript

input is print(make_dict("test is test tests tests tests")) output is {'test': 2, 'is': 1, 'tests': 3}

im search output tests ,test ,is

Advertisement

Answer

You can change your code like approach_1 or use collections.Counter like approach_2.

  1. You can sorted on dict.items() and return result as dict
  2. Use Counter and return most_common.

Approach_1

JavaScript

Approach_2

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement