你好,你可以考虑使用下面的代码:
inputStr=“adhdbxhsjjsbbsxjdjjebbxbsheuhdbbd”
ResultDict={}
for index in range(len(inputStr)):
if inputStr[index] in ResultDict:
ResultDict[inputStr[index]] +=1
else:
ResultDict[inputStr[index]]=1
for each in ResultDict:
print(each +” “ +str(ResultDict[each]))
追问怎么按照字母出现的频率从大到小输出啊
追答你好,更新的代码和结果如下
inputStr="adhdbxhsjjsbbsxjdjjebbxbsheuhdbbd"
ResultDict={}
for index in range(len(inputStr)):
if inputStr[index] in ResultDict:
ResultDict[inputStr[index]] +=1
else:
ResultDict[inputStr[index]]=1
for each in sorted(ResultDict.keys()):
print(each + " " +str(ResultDict[each]))
print (sorted(ResultDict.items(), key=lambda x: x[1]))
第一排序结果
a 1
b 8
d 5
e 2
h 4
j 5
s 4
u 1
x 3
第二排序的结果
[('a', 1), ('u', 1), ('e', 2), ('x', 3), ('h', 4), ('s', 4), ('d', 5), ('j', 5), ('b', 8)]
你好,更新的代码和结果如下
inputStr="adhdbxhsjjsbbsxjdjjebbxbsheuhdbbd"
ResultDict={}
for index in range(len(inputStr)):
if inputStr[index] in ResultDict:
ResultDict[inputStr[index]] +=1
else:
ResultDict[inputStr[index]]=1
for each in sorted(ResultDict.keys()):
print(each + " " +str(ResultDict[each]))
print (sorted(ResultDict.items(), key=lambda x: x[1]))
第一排序结果
a 1
b 8
d 5
e 2
h 4
j 5
s 4
u 1
x 3
第二排序的结果
[('a', 1), ('u', 1), ('e', 2), ('x', 3), ('h', 4), ('s', 4), ('d', 5), ('j', 5), ('b', 8)]