Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

I'm attempting to tally the occurrences of each and every letter of a word 

word = input("Enter a word")

Alphabet=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

for i in range(0,26):

    print(word.count(Alphabet[i]))

This as of now yields the no. of times each letter that happens including the ones that don't. 

How would I list the letters vertically with the frequency close by it, e.g., like the accompanying?

word="Hello"

H 1

E 1

L 2

O 1

1 Answer

0 votes
by (26.4k points)

Have a try at utilizing Counter, which will make a dictionary that contains the frequencies of all items in an assortment(Collection). 

from collections import Counter

counts=Counter(word) # Counter({'l': 2, 'H': 1, 'e': 1, 'o': 1})

for i in word:

    print(i,counts[i])

Else, you could do a condition on your present code to print in particular if word.count(Alphabet[i]) is more prominent than 0, however that would be more slow.

Interested to learn python in detail? Come and Join the python course.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...