+ 1

letter counter

here is my code,what should i do that if a letter doesn't exist in the input word,my code instead of showing that chracter count is zero, just skip it. x = list(input()) y = { "a":x.count("a"),"b":x.count("b"),"c":x.count("c"), "d":x.count("d"),"e":x.count("e"), "f":x.count("f"),"g":x.count("g"),"h":x.count("h"), "i":x.count("i"),"j":x.count("j"), "k":x.count("k"),"l":x.count("m"),"n":x.count("n"), "o":x.count("o"),"p":x.count("p"), "q":x.count("q"),"r":x.count("r"),"s":x.count("s"), "t":x.count("t"),"u":x.count("u"), "v":x.count("v"),"w":x.count("w"),"x":x.count("x"), "y":x.count("y"),"z":x.count("z") } print(y) sample input: "academy" my output: {'a': 2, 'b': 0, 'c': 1, 'd': 1, 'e': 1, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 1, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 1, 'z': 0} expected output: {'a':2,'c':1,'d':1,'e':1,'l':1,'y':1}

17th Sep 2021, 6:53 AM
Alireza Azizzadeh
Alireza Azizzadeh - avatar
2 Réponses
+ 1
Here are a couple of examples of how you can do this. https://code.sololearn.com/cSOtMUXB3k5u/?ref=app
17th Sep 2021, 7:06 AM
ChaoticDawg
ChaoticDawg - avatar
0
I don't know how to do it with python but try doing this in python: Go through the string/list from 0 to string/list size. Count all occurances of the index you are at(0,1,2...size - 1) and delete all occurances but first occurance.
17th Sep 2021, 7:12 AM
Mani_K_A