I have a list of strings, like so:

{"aa bb", "cc dd", "aa bb", "aa bb", "cc dd", "ww ss", "ss ss", "kk mm"}

I know WordCounts will return the numbers of every single word, but I want to the word group numbers, i.e., "aa bb" 3 times, and "cc dd" 2 times. How can I do this?


You can try Tally

lst = {"aa bb", "cc dd", "aa bb", "aa bb", "cc dd", "ww ss", "ss ss",   "kk mm"};

Mathematica graphics

Edit by m_goldberg

As J.M. says in his comment below, Counts will give the same information as an association.


<|"aa bb" -> 3, "cc dd" -> 2, "ww ss" -> 1, "ss ss" -> 1, "kk mm" -> 1|>

This is equivalent to

Rule @@@ Tally[data] // Association
  • 2
    $\begingroup$ Counts[] should also work. $\endgroup$ – J. M.'s ennui Mar 4 '19 at 5:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.