a, b = {}, ''
txt = input("输入:")
for i in "!@#$%^&*(){:'}?/|\_,.<>;`~-=+": # 去除特殊符号
txt = txt.replace(i, '')
for j in txt: # 筛选小写字母
if ord('a') <= ord(j) <= ord('z'):
a[j] = a.get(j, 0)+1
items = list(a.items())
items.sort(key=lambda x: ord(x[0])) # 按照字母表顺序输出
for c in items:
b += "{}:{},".format(c[0], c[1])
print("输出:{}".format(b[:-1]))
代码改进
a, b = {}, ''
txt = input("输入:")
for j in txt: # 筛选小写字母,去除特殊符号。
if 'a' <= j <= 'z':
a[j] = a.get(j, 0)+1
items = list(a.items())
items.sort(key=lambda x: x[0]) # 按照字母表顺序输出
for c in items:
b += "{}:{},".format(c[0], c[1])
print("输出:{}".format(b[:-1]))