-3

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?

Karnivaurus
  • 22,823
  • 57
  • 147
  • 247

1 Answers1

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)]
Community
  • 1
  • 1
Falko
  • 17,076
  • 13
  • 60
  • 105