Python一日一练19----统计字符串中的字符个数

来源:互联网 发布:cross over软件下载 编辑:程序博客网 时间:2024/05/21 23:00

要求

题目内容:
定义函数countchar()统计字符串中所有出现的字母的个数(允许输入大写字符,并且计数时不区分大小写)。

输入格式:
字符串

输出格式:
列表

输入样例:
Hello, World!

输出样例:
[0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 3, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0]

代码

#coding=utf-8__author__ = 'a359680405'def countchar(str):    charmap={}    #初始化字典    for i in range(26):        charmap[chr(i+65)]=0    #统一大小写    str=str.upper()    #统计个字母出现次数    for c in str:        if  ord("A")<=ord(c)<=ord("Z"):            charmap[c]+=1        else:            continue    return [charmap[chr(i+65)] for i in range(26)]#测试一下print(countchar("abcdefghijklmnopqrstuvwxyz       ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()0987654321"))

本文地址:http://blog.csdn.net/a359680405/article/details/51282332

0 0