PL/Python

[Python] str안에 alphabet 각각의 개수 구하기

SweetDev 2021. 7. 3. 21:53

안타깝게도 바로 a몇개, b몇개 알려주는 함수는 없지만 구할 수는 있다!!

string = input()

Alphabet = 'abcdefghijklmnopqrstuvwxyz'

pt_freq = [0] * 26

for ch in string:
    if ch in Alphabet:
        idx = Alphabet.find(ch)
        pt_freq[idx] += 1


for i in range(0,26):
    print(pt_freq[i], end=" ")

스트링 한글자씩 돌면서, 알파벳 집합에 대해서 index를 찾아서 그 값을 추가해준다.