当前位置:首页 > Python教程 > python技巧

一些Python代码

统计字符串中的字符个数。未通过

            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))

 以下为通过代码,注意字典是无序的

            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))

 

原文:https://www.cnblogs.com/candyYang/p/11621438.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐