+ 5

LETTER COUNTER

Given a string as input, you need to output how many times each letter appears in the string. You decide to store the data in a dictionary, with the letters as the keys, and the corresponding counts as the values. Create a program to take a string as input and output a dictionary, which represents the letter count. Sample Input: hello Sample Output: {'h': 1, 'e': 1, 'l': 2, 'o': 1} Please help me to solve this problem.

13th Feb 2021, 4:36 AM
P. SAI SRI RAM
P. SAI SRI RAM - avatar
15 Antworten
13th Feb 2021, 6:35 PM
Kingsley Aham
Kingsley Aham - avatar
+ 10
Show your attempt first. Then we'll help you to correct it.
13th Feb 2021, 4:38 AM
K.S.S. Karunarathne
K.S.S. Karunarathne - avatar
+ 9
text = input() dict = {} for char in text: counts = text.count(char) dict[char] = counts print(dict)
14th Jul 2021, 8:49 PM
Seyed Amin Naghavi
Seyed Amin Naghavi - avatar
+ 4
Don't forget about return in the function
13th Feb 2021, 5:09 AM
Alexander Koval🇧🇾
Alexander Koval🇧🇾 - avatar
13th Feb 2021, 5:08 AM
Alexander Koval🇧🇾
Alexander Koval🇧🇾 - avatar
+ 3
Thanks all of you!!!!!
13th Feb 2021, 5:09 AM
P. SAI SRI RAM
P. SAI SRI RAM - avatar
+ 3
K.S.S. KARUNARATHNE you are the best!
13th Feb 2021, 5:14 AM
Alexander Koval🇧🇾
Alexander Koval🇧🇾 - avatar
+ 3
https://code.sololearn.com/c6T30ieQcUx5 Add pairs to the dictionary using the "get(key [, default])" function, which looks up the key [i] (the letters at, which you want to add). If it is not, adds a pair, key = letter [i] with a default value = 0 (instead of "None" so that you can do mathematical operations) and append to the value 1 (+1). If the key is already in the dictionary, just adds 1 (+1) to the value. >>> text = input() dict = {} for i in text: dict[i] = dict.get(i,0) + 1 print(dict) Another way: >>> text = input() dict = {a:b for a in text for b in str(text.count(a))} print(dict)
6th Dec 2021, 8:32 AM
Maksim Guskov
Maksim Guskov - avatar
+ 2
Try this text = input() dict = {} for letter in text: dict.__setitem__(letter,text.count(letter)) print(dict)
13th Feb 2021, 4:52 AM
K.S.S. Karunarathne
K.S.S. Karunarathne - avatar
+ 1
That all
13th Feb 2021, 4:45 AM
P. SAI SRI RAM
P. SAI SRI RAM - avatar
+ 1
I was completely stumped on this project as well, thanks!
23rd Feb 2021, 12:54 AM
Mark Evans
Mark Evans - avatar
0
text = input() dict = {} #your code goes here def dicting(text) for char in text: if char not in text: dict += ch
13th Feb 2021, 4:45 AM
P. SAI SRI RAM
P. SAI SRI RAM - avatar
0
text = input() dict = {} for char in text: if char not in dict: dict[char]=1 else: dict[char] += 1 print(dict)
5th Jan 2022, 6:13 AM
Ramin
Ramin - avatar
0
text = input() dict = {} for x in text: dict[x] = dict.get(x,0) if x in dict: dict[x] += 1 print(dict)
20th Mar 2022, 5:59 PM
林宗穎
林宗穎 - avatar