用python编写一个函数tongji(s),接收传入的字符串s,统计大写字母的个数、 小写字母的
发布网友
发布时间:2022-05-03 04:37
我来回答
共1个回答
热心网友
时间:2023-10-09 18:30
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