[Python] Create a program that takes a string as input and output a dictionary representing the letter count | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

[Python] Create a program that takes a string as input and output a dictionary representing the letter count

It's the end of module project of the intermediate python course. My code passed the first two tests but fails the last two and I don't know why. If anyone can help, here's my code: text = input() dict = {} letter_list = [] text=text.lower() for x in text: if x not in letter_list: letter_list.append(x) count=0 counts=[] for x in text: for y in text: if x == y: count+=1 text=text.replace(x,"") counts.append(count) count=0 for x in counts: if x ==0: counts.remove(x) for i in range(len(letter_list)): if letter_list[i] not in [" ","-","'","!","?"]: dict[letter_list[i]]=counts[i] print(dict)

3rd Mar 2021, 10:03 AM
Will - avatar
17 Answers
+ 18
mydict={} for l in input(): try: mydict[l]+=1 except: mydict[ l]=1
3rd Mar 2021, 11:30 AM
Oma Falk
Oma Falk - avatar
+ 8
Use dictionary comprehension # read input <text> text = input() # collect unique characters in a set unique_chars = set( text ) # build a dictionary with the character as key # and frequency of the character as value stat = { c : text.count( c ) for c in unique_chars } # print dictionary print( stat )
3rd Mar 2021, 10:16 AM
+ 8
word = input() d = dict.fromkeys(word, 0) for s in word: d[s] += 1
3rd Mar 2021, 2:07 PM
Vitaly Sokol
Vitaly Sokol - avatar
+ 7
for letter in text: dict[letter] = dict.get(letter, 0) + 1 This for loop will loop through the letters in your string and if the letter has not been added to the dictionary yet, it will add it and change the count to 1, otherwise it will increment the count. I think this is what youre trying to do correct?
5th Mar 2021, 12:11 AM
NazraT - avatar
+ 6
text = input() dict = {} for l in text: if l in dict: dict[l]+=1 else: dict[l]=1 print(dict) But I like Frogged and Jan Markus [PRO_crastinator] solutions better
3rd Mar 2021, 1:17 PM
Paul K Sadler
Paul K Sadler - avatar
+ 5
Bassel Selim Alamir Hanna In that case, need to import collections module and use OrderedDict https://code.sololearn.com/cnHJSGBD2W87/?ref=app
3rd Mar 2021, 11:24 AM
+ 5
Paul K Sadler I always love posts with many ways to solve a tiny challenge. Would not like to miss yours.
3rd Mar 2021, 1:20 PM
Oma Falk
Oma Falk - avatar
+ 3
Ipang I forgot to specify that that the letters in the dictionary need to be in the order of appearance in the string.
3rd Mar 2021, 10:57 AM
Will - avatar
+ 2
Jan Markus [PRO_crastinator] Counter looks like it orders the dictionary by count of occurrences, instead of by order of occurrence. Very handy, but not what Bassel Selim Alamir Hanna asked for
5th Mar 2021, 6:03 AM
Steve - avatar
+ 1
a, b = input(), {} for x in a: b[x] = a.count[x] // Hope this executes fast
4th Mar 2021, 5:22 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Letter counter create an empty dictionary Iteriate over the characters of the string When the characte is alphaberic, use count to count( ) how many times the character appears then use reduce( ) to remove the character from the string add the character to the dictionary text = input() dict = {} #your code goes here for character in text: if character.isalpha(): dict[character] = text.count(character) text.replace(character,"") print(dict)
27th Jul 2021, 8:20 PM
Mwaniki Grace Waigumo
Mwaniki Grace Waigumo - avatar
+ 1
text = str(input()) dict = {} for letter in text : if letter in dict: dict[letter] +=1 else: dict[letter] =1 print(dict)
10th Aug 2022, 10:13 PM
Abdul Ghafar
Abdul Ghafar - avatar
text = input() v=[b for b in text] dict={} for i in v: if i in dict: dict[i]+=1 else: dict[i]=1 print(dict)
5th Mar 2021, 12:07 AM
FarhanHajii - avatar
text = input() # take input dict = {} # make an empty dictionary for i in text: # looping from start of input string to end if i in dict: # if letter found in dictionary then adding 1 to existing value dict[i] += 1 else: # if letter not found then making frequency of current letter 1 dict[i] = 1 print(dict) # Printing the Dictionary
3rd May 2021, 3:08 AM
Chirag Gajbhiye
Chirag Gajbhiye - avatar
4th Mar 2021, 10:16 AM
shally lu
- 3
5th Mar 2021, 2:39 AM
juan pablo mt dos santos
juan pablo mt dos santos - avatar
- 3
txt = str(input()) reverse = txt[::-1] last = reverse[0] print (last) done...!
5th Mar 2022, 3:38 PM