I have a list of strings, X
. Each entry x_i
in X
is one from a set of possible strings Y
. How can I count the number of instances of string y_j
in X
?
Asked
Active
Viewed 154 times
-3

Karnivaurus
- 22,823
- 57
- 147
- 247
1 Answers
2
A solution from here:
from collections import Counter
X = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
print Counter(X).items()
Output:
[('blue', 3), ('yellow', 1), ('red', 2)]
-
Thanks, but what if I don't know the set `Y` in advance? – Karnivaurus Aug 10 '14 at 20:55
-
For this solution you don't need to know ```Y```. – Falko Aug 10 '14 at 20:57