给定任务:需要从用户那里得到一个词,然后必须统计该词中的总字符并按排序顺序显示(计数必须降序,字符必须升序 - 即,如果用户给出“管理”,那么输出应该是
**a 2
e 2
m 2
n 2
g 1
t 1**
这是我为该任务编写的代码:
string=input().strip()
set1=set(string)
lis=[]
for i in set1:
lis.append(i)
lis.sort()
while len(lis)>0:
maxi=0
for i in lis:
if string.count(i)>maxi:
maxi=string.count(i)
for j in lis:
if string.count(j)==maxi:
print(j,maxi)
lis.remove(j)
这段代码为我提供了字符串“管理”的以下输出
a 2
m 2
e 2
n 2
g 1
t 1
m & e 未排序。我的代码有什么问题?