用python编写一个函数tongji(s),接收传入的字符串s,统计大写字母的个数、 小写字母的
用python编写一个函数tongji(s),接收传入的字符串s,统计大写字母的个数、小写字母的个数、数字的个数、其他字符的个数...
用python编写一个函数tongji(s),接收传入的字符串s,统计大写字母的个数、 小写字母的个数、数字的个数、其他字符的个数
展开
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
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
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询