Some Python code

Count the number of characters in a string. Did not pass

def countchar(string):
    c_dict = {}
    for i in range(26):
        c_dict[chr(ord('a')+i)] = 0
    for c in string:
        if c in c_dict:
            c_dict[c] += 1
    return list(c_dict.values())
if __name__ == "__main__":
    string = input()
    string = string.lower()
    print(countchar(string))

 The following is through code, note that dictionaries are unordered

def countchar(string):
    c_dict = {}
    c_list = []
    for i in range(26):
        c_dict[chr(ord('a')+i)] = 0
    for c in string:
        if c in c_dict:
            c_dict[c] += 1
    c_list = c_dict.items()
    c_list= sorted(c_list, key = lambda x:x[0])
    c_list = [x[1] for x in c_list]

    return c_list
if __name__ == "__main__":
    string = input()
    string = string.lower()
    print(countchar(string))

 

Guess you like

Origin www.cnblogs.com/candyYang/p/11621438.html