用python编写一个函数tongji(s),接收传入的字符串s,统计大写字母的个数、 小写字母的

用python编写一个函数tongji(s),接收传入的字符串s,统计大写字母的个数、 小写字母的个数、数字的个数、其他字符的个数

第1个回答  2016-05-28
import string
def chartype(ch):
if ch in string.ascii_letters: return 'ascii_letters'
elif ch in string.digits: return 'digits'
elif ch in string.whitespace: return 'whitespace'
else: return 'other'

def iterchtypecount(s):
counter = {}
for c in s:
counter.setdefault(chartype(c), []).append(c)
for t, lst in counter.items():
yield t, len(lst)

for chtype, cnts in iterchtypecount(raw_input("Enter a string: ")):
print chtype, cnts本回答被提问者采纳
相似回答