In Python, I’ve got a list of items like:
mylist = [a, a, a, a, b, b, b, d, d, d, c, c, e]
And I’d like to output something like:
a (4)
b (3)
d (3)
c (2)
e (1)
How can I output a count and leaderboard of items in a list? I’m not too bothered about efficiency, just any way that works 🙂
Thanks!
So this function uses a
defaultdictto count the number of each entry in our list. We then take each pair of the entry and its count and sort it in descending order according to the count. We then take thetopnumber of entries and return that.So now we can say