|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
从键盘输入一串字符,统计输出出现次数最多的前3个字符及次数,并输出总共出现了多少种字符
str=input()
space=0
number=0
zimu=0
other=0
m=0
for i in str:
if i.isspace():
space=space+1
elif i.isalpha():
zimu=zimu+1
elif i.isnumeric():
number=number+1
else:
other=other+1
if space>0:
m=m+1
if zimu>0:
m=m+1
if number>0:
m=m+1
if other>0:
m=m+1
print("字母有{}空格有{}其他有{}数字有{}总共出现了{}种字符".format(zimu,space,other,number,m))
感觉做得不对 跟题目不对应 字符数应该不应该像我这样分类吧 另外不知道怎么统计前三的字符
- # -*- coding: utf-8 -*-
- def statistics(string):
- appear_dict = {}
- for i in string:
- if i not in appear_dict:
- appear_dict[i] = 1
- else:
- appear_dict[i] += 1
- return appear_dict.items()
- str1 = input("请输入字符:")
- appear = statistics(str1)
- appear = list(sorted(appear, key=lambda x: x[1], reverse=True))
- print("出现最多的 3 种字符分别是:")
- for i in range(3):
- try:
- print(f"{appear[i][0]} : {appear[i][1]}")
- except IndexError:
- break
- print(f"一共出现了 {len(appear)} 种字符")
复制代码
|
|