Use Python dictionary to obtain the number of alphanumeric characters in the input string

Asked 2 years ago, Updated 2 years ago, 15 views

Ask about the code that outputs the number of alphanumeric characters entered using dictionary, such as 'o': 3, 'g': 2, '.': 1, 'e': 1, 'l': 1, 'm': 1, and 'c': 1.

python

2022-09-20 19:32

1 Answers

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

Coding is Nogada. Haha


2022-09-20 19:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.