Given:
mystr = "AHjksHslknHKJBSEW-QLM +sdfkjbKJGEmn";
From version 10 onward, we can get a count of occurrences for each character like this:
count = mystr // Characters // Counts
(* <| "A" -> 1, "H" -> 3, "j" -> 2, "k" -> 3, "s" -> 3, "l" -> 1,
"n" -> 2, "K" -> 2, "J" -> 2, "B" -> 1, "S" -> 1, "E" -> 2,
"W" -> 1, "-" -> 1, "Q" -> 1, "L" -> 1, "M" -> 1, " " -> 1,
"+" -> 1, "d" -> 1, "f" -> 1, "b" -> 1, "G" -> 1, "m" -> 1 |> *)
If we want these in order, we can sort:
count // Sort
(* <| "-" -> 1, "+" -> 1, " " -> 1, "A" -> 1, "b" -> 1, "B" -> 1,
"d" -> 1, "f" -> 1, "G" -> 1, "l" -> 1, "L" -> 1, "m" -> 1,
"M" -> 1, "Q" -> 1, "S" -> 1, "W" -> 1, "E" -> 2, "j" -> 2,
"J" -> 2, "K" -> 2, "n" -> 2, "H" -> 3, "k" -> 3, "s" -> 3 |> *)
We can also use count
to obtain the counts of individual characters:
count["A"]
(* 1 *)
count["H"]
(* 3 *)
Prior to Version 10
Prior to version 10, we can use Tally
and Rule
to get the same effect:
count2 = mystr // Characters // Tally // Rule @@@ # &
(* { "A" -> 1, "H" -> 3, "j" -> 2, "k" -> 3, "s" -> 3, "l" -> 1,
"n" -> 2, "K" -> 2, "J" -> 2, "B" -> 1, "S" -> 1, "E" -> 2,
"W" -> 1, "-" -> 1, "Q" -> 1, "L" -> 1, "M" -> 1, " " -> 1,
"+" -> 1, "d" -> 1, "f" -> 1, "b" -> 1, "G" -> 1, "m" -> 1 } *)
"E" /. count2
(* 2 *)
CharacterCounts (Version 10.1+)
As @yode points out in a comment, version 10.1 introduced the helper function LetterCounts
. It also introduced CharacterCounts
:
mystr // CharacterCounts
(* <| "s" -> 3, "k" -> 3, "H" -> 3, "n" -> 2, "K" -> 2, "J" -> 2,
"j" -> 2, "E" -> 2, "W" -> 1, "S" -> 1, "Q" -> 1, "M" -> 1,
"m" -> 1, "L" -> 1, "l" -> 1, "G" -> 1, "f" -> 1, "d" -> 1,
"B" -> 1, "b" -> 1, "A" -> 1, " " -> 1, "+" -> 1, "-" -> 1 |> *)
Note that the result is sorted in descending order by frequency. The documentation does not mention this fact, but an inspection of the definition of CharacterCounts
reveals that it is intentional. CharacterCounts
also supports the IgnoreCase
keyword and counting n-grams:
CharacterCounts[mystr, 2, IgnoreCase -> True]
(* <| "ah" -> 1, "hj" -> 1, "jk" -> 1, "ks" -> 1, "sh" -> 1,
"hs" -> 1, "sl" -> 1, "lk" -> 1, "kn" -> 1, "nh" -> 1,
"hk" -> 1, "kj" -> 3, "jb" -> 2, "bs" -> 1, "se" -> 1,
"ew" -> 1, "w-" -> 1, "-q" -> 1, "ql" -> 1, "lm" -> 1,
"m " -> 1, " +" -> 1, "+s" -> 1, "sd" -> 1, "df" -> 1,
"fk" -> 1, "bk" -> 1, "jg" -> 1, "ge" -> 1, "em" -> 1,
"mn" -> 1 |> *)
StringPartition
andTally
? $\endgroup$ – bill s Feb 13 '17 at 2:40