파이썬 dictionary를 사용해서 입력받은 문자열의 알파벳 개수를 구하기

조회수 1744회

dictionary를 사용해서 입력받은 문자열의 알파벳 개수를 'o': 3, 'g': 2, '.': 1, 'e': 1, 'l': 1, 'm': 1, 'c': 1과 같이 출력하는 코드에 대해서 물어봅니다.

  • 숙제는 스스로 하세요. nowp 2020.11.18 14:03

1 답변

  • def alph(string):
        list1 = []
        for i in range(len(string)):
            list1.append(string[i])
        a_c = list1.count('a')
        b_c = list1.count('b')
        c_c = list1.count('c')
        d_c = list1.count('d')
        e_c = list1.count('e')
        f_c = list1.count('f')
        g_c = list1.count('g')
        h_c = list1.count('h')
        i_c = list1.count('i')
        j_c = list1.count('j')
        k_c = list1.count('k')
        l_c = list1.count('l')
        m_c = list1.count('m')
        n_c = list1.count('n')
        o_c = list1.count('o')
        p_c = list1.count('p')
        q_c = list1.count('q')
        r_c = list1.count('r')
        s_c = list1.count('s')
        t_c = list1.count('t')
        u_c = list1.count('u')
        v_c = list1.count('v')
        w_c = list1.count('w')
        x_c = list1.count('x')
        y_c = list1.count('y')
        z_c = list1.count('z')
        list2 =[a_c,b_c,c_c,d_c,e_c,f_c,g_c,h_c,i_c,j_c,k_c,l_c,m_c,n_c,o_c,p_c,q_c,r_c,s_c,t_c,u_c,v_c,w_c,x_c,y_c,z_c]
        list3 = ['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']
        dic = dict(zip(list3,list2))
        return dic
    

    코딩은 노가다입니다. ㅎㅎ

    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)