Given a List perform reordering to get similar elements in consecution.
Input : test_list = [4, 7, 5, 4, 1, 4, 1, 6, 7, 5]
Output : [4, 4, 4, 7, 7, 5, 5, 1, 1, 6]
Explanation : All similar elements are assigned to be consecutive.
Input : test_list = [4, 7, 5, 1, 4, 1, 6, 7, 5]
Output : [4, 4, 7, 7, 5, 5, 1, 1, 6]
Explanation : All similar elements are assigned to be consecutive.
In this, we perform the task of computing frequency using Counter(), and loop and items() are used to reorder elements according to count, and access frequencies respectively.
In this, we perform the task of reordering the counted frequencies using elements(), providing a concise solution.
OutputThe original list is : [4, 7, 5, 4, 1, 4, 1, 6, 7, 5]
Reordered List : [4, 4, 4, 7, 7, 5, 5, 1, 1, 6]