Counting Letter Frequency in a String (Python)

后端 未结 12 2116
既然无缘
既然无缘 2020-12-01 17:52

I am trying to count the occurrences of each letter of a word

word = input(\"Enter a word\")

Alphabet=[\'a\',\'b\',\'c\',\'d\',\'e\',\'f\',\'g\',\'h\',\'i\'         


        
12条回答
  •  爱一瞬间的悲伤
    2020-12-01 18:25

    Initialize an empty dictionary and iterate over every character of the word. If the current character in present in the dictionary, increment its value by 1, and if not, set its value to 1.

    word="Hello"
    characters={}
    for character in word:
        if character in characters:
            characters[character] += 1
        else:
            characters[character] =  1
    print(characters)
    

提交回复
热议问题