首页 > 编程语言 > 详细

一些Python代码

时间:2019-10-04 10:58:54      阅读:80      评论:0      收藏:0      [点我收藏+]

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

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

 

一些Python代码

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!