I’m very new to Python and do know that my question is very simple but I’ve not found an existed question on SO yet.
I have an array contains string elements. Now I want to extract elements and count the number of appearances of them, them sort in descending order.
For example:
['ab' 'ab' 'ac']
then the output should be:
'ab' 2
'ac' 1
Also, it’s bad of me that I don’t know what is the best way to store my output (in a map, hash… or something like that? Again, I’m not sure)…
Thanks for any help.
This can be done using the
Counterclass from thecollectionsmodule.countsstores the count information for each element; the full list of methods can be found in the documentation, but probably all you care about is that you can access counts directly by treatingcountslike a hash: