Counting each letter's frequency in a string
In 2.7+:
import collections
letters = collections.Counter('google')
Earlier (2.5+, that's ancient by now):import collections
letters = collections.defaultdict(int)
for letter in word:
letters[letter] += 1
how to count the frequency of letters in text excluding whitespace and numbers?
This should work:
>>> from collections import Counter
>>> from string import ascii_letters
>>> def count_letters(s) :
... filtered = [c for c in s.lower() if c in ascii_letters]
... return Counter(filtered)
...
>>> count_letters('Math is fun! 2+2=4')
Counter({'a': 1, 'f': 1, 'i': 1, 'h': 1, 'm': 1, 'n': 1, 's': 1, 'u': 1, 't': 1})
>>>
Python - Counting Letter Frequency in a String
Does this satisfy your needs?
from itertools import groupby
s = "bbbaaac ddddee aa"
groups = groupby(s)
result = [(label, sum(1 for _ in group)) for label, group in groups]
res1 = "".join("{}{}".format(label, count) for label, count in result)
# 'b3a3c1 1d4e2 1a2'
# spaces just as spaces, do not include their count
import re
re.sub(' [0-9]+', ' ', res1)
'b3a3c1 d4e2 a2'
Counting frequency of characters in a string using JavaScript
Here you go:
function getFrequency(string) {
var freq = {};
for (var i=0; i<string.length;i++) {
var character = string.charAt(i);
if (freq[character]) {
freq[character]++;
} else {
freq[character] = 1;
}
}
return freq;
};
What's the most efficient way to calculate frequency of each letter in a string C++?
You can omit std::set
and just write like so
int main() {
std::string s;
std::cin >> s;
int cnt[26] = {};
for (int i = 0; i < s.length(); i++) {
cnt[s[i] - 'a']++;
}
for (int i = 0; i < 26; ++i)
if (cnt[i] > 0)
std::cout << (char)('a' + i) << " : " << cnt[i] << std::endl;
return 0;
}
Instead saving in the std::set
, we check the presence of a character in the cnt
and to output if a symbol was.This option takes less memory.
Counting Character Frequency in String(Java)
Well, count
is an int[]
with 256 slots:
int count[] = new int[MAX_CHAR]; // MAX_CHAR is 256
Your algorithm defines MAX_CHAR = 256
, because it assumes the string consists only of 8-Bit ASCII characters.[0, 0, ..., 0, 0] // 256 slots
Now you're iterating each character in string str
and cast it to an integer (see type casting of primitives in Java). An A
will be casted to 65 (ASCII table), a B
to 66 and so on. The casted int
is the slot to increment. So a string A
would lead to an increment of the integer at index 65. Your question was primarily aboutcount[str.charAt(i)]++
That translates to this:char c = str.charAt(i); // c = A
int index = c; // c = A, casted to an int = 65
count[index]++ // increments the int at position 65
Result:[0, 0, ..., 1, ..., 0, 0]
^ index 65
The next A
would increment the int at index 65 again:[0, 0, ..., 2, ..., 0, 0]
^ index 65
count the frequency of each character using the dictionary in python
You can iterate over string and update the dictionary accordingly and also there's no need of any count variable.
test_str = input().lower()
dicx = {}
for i in test_str:
if i in dicx:
dicx[i] += 1
else:
dicx[i] = 1
print(dicx)
Related Topics
Unexpected Results Converting Timezones in Python
How to Have Shared Log Files Under Windows
How to Save and Restore Multiple Variables in Python
Cartesian Product of a Dictionary of Lists
How to Plot Only a Table in Matplotlib
How to Make a Local Variable (Inside a Function) Global
Is It Better to Use "Is" or "==" for Number Comparison in Python
Installing Pygraphviz on Windows 10 64-Bit, Python 3.6
"Permission Denied" Trying to Run Python on Windows 10
Python - Datetime with Timezone to Epoch
How to Write a Generator Class
Pairwise Crossproduct in Python
Wrapping Around on a List When List Index Is Out of Range