第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本回答被提问者采纳