Python 请定义一个函数count(s, c),它检查字符串s中单个字符c出现的次数? 5
请定义一个函数count(s,c),它检查字符串s中单个字符c出现的次数,返回这个次数用python...
请定义一个函数count(s, c),它检查字符串s中单个字符c出现的次数,返回这个次数
用python 展开
用python 展开
1个回答
展开全部
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
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询