I have a list of strings and some of them are equal. I need some script which would count equal strings. Ex:
I have a list with some words :
"House"
"Dream"
"Tree"
"Tree"
"House"
"Sky"
"House"
And the output should look like this:
"House" - 3
"Tree" - 2
"Dream" - 1
and so on
Use collections.Counter(). It is designed for exactly this use case:
>>> import collections
>>> seq = ["House", "Dream", "Tree", "Tree", "House", "Sky", "House"]
>>> for word, cnt in collections.Counter(seq).most_common():
print repr(word), '-', cnt
'House' - 3
'Tree' - 2
'Sky' - 1
'Dream' - 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With