Problem-Solving ideas:
1. Divide the string s into blank characters to get all the word lists split_s, such as: [' Betty ', ' bought ', ' a ', ' bit ', ' of ', ' butter ', ' but ', ' the ', ' butter ', ' is ', ' bitter ' ]
2. Establish maplist and convert the split_s into elements as a list of tuples, such as: [(' Betty ', 1), (' bought ', 1), (' A ', 1), (' A ', 1), (' of ', 1), (' of ', "1 '), (' but '), 1), (' The ', 1), (' Butter ', 1), (' was ', 1), (' Bitter ', 1)]
3. Merge elements in Maplist, the first index value of the tuple is the same, then its second index value is added.
Note: Prepare to use defaultdict. The data obtained are as follows: {' Betty ': 1, ' bought ': 1, ' A ': 1, ' bit ': 1, ' of ': 1, ' Butter ': 2, ' but ': 1, ' the ': 1, ' is ': 1, ' Bitter ': 1}
4. Sort the alphabetical order by key and get the following: [(' A ', ' 1 '), (' Betty ', 1), (' bit ', 1), (' Bitter ', 1), (' bought ', 1), (' but ', 1), (' Butter ', 2), (' O F ', 1), (' The ', 1), (' was ', 1)]
5. Two orders, sorted by value, are as follows: [(' Butter ', 2), (' A ', 1), (' Betty ', 1), (' bit ', 1), (' Bitter ', 1), (' bought ', 1), (' but ', 1), (' of ', 1), (' The ', ' 1 '), (' was ', 1)]
6. Use the slices to remove the higher frequency * Group data
Summary: It is also correct to sort the results on Python3 without defaultdict, Python2 is incorrect. The defaultdict itself is not sequential and must be sorted to differentiate the list.
You can also try to write yourself without using third-party modules