+ 1
Please tell me how to calculate letter frequency..Ex-wife You have word discipline. Calculate frequency of I in the word.
4 Réponses
+ 8
Apollo-Roboto ,
normally the asker of a question should post his attempt here, before he will get help. this is because we belive that just giving a ready made code does NOT really help you.
cheers also to Isaac Fernandes
besides of this, the code can be done with less complexity, just using string.count()
# no need to split the input string to individual chars
# no need for a nested loop
def letter_frequency(txt, char):
print(txt.count(char))
letter_frequency("My name is Lothar", "a")
+ 2
letter count and letter frequency are two different things (even if related) ^^
letter count is the absolute number of occurence, while frequency is that number divided by the total number of letters (length)...
assuming you don't differentiate letters from other chars in a string (digits, punctuation, spaces...):
+ count of a specific char as previously said is simply given by string.count(char)...
+ frequency is given by string.count(char)/len(string)
0
show your attemps so we can help you out
0
Pretty simple logic, but please attach your code here next time, so we can guide you through writing the code yourself. If you tried and got stuck, here's the code:
def letterFrequency(str, char):
arr = str.split(" ");
count = 0;
for word in arr:
for letter in word:
if letter == char:
count +=1
print(count)
letterFrequency("My name is Gavin","a")
Output:
2
Cheers✌