Reputation: 605
I have a list
["Alfa", "Beta", "Gamma", "Beta", "Alpha"]
I need to count the repetion of elements in this list and to put the result in a list of tuples in descending order.
If the count of two of the elements is the same, I need to sort them in reverse alphabetical order (Z->A)
This is the expected output
[('Beta', 2), ('Alfa', 1), ('Alpha', 1), ('Gamma', 1)]
My idea is to start splitting the string as following
def count_items(string):
wordlist = string.split(' ')
But I have no idea of how to continue.
Could you please help?
Upvotes: 0
Views: 107
Reputation: 22697
You can use Counter
built module along most_common
method
from collections import Counter
>>> l = ["Alfa", "Beta", "Gamma", "Beta", "Alpha"]
>>> Counter(l).most_common()
>>> [('Beta', 2), ('Alfa', 1), ('Alpha', 1), ('Gamma', 1)]
Upvotes: 1