python 中统计字符串中每个字符出现的次数

 

001、

>>> str1 = "abeababxxxaacdx"
>>> dict1 = {}
>>> for i in str1:                     ## 利用循环结构,将字符串中每个字符出现的次数储存在字典中
...     dict1[i] = str1.count(i)
...
>>> for i in dict1:                    ## 输出统计结果
...     print(i, dict1[i])
...
a 5
b 3
e 1
x 4
c 1
d 1

 

posted @ 2022-08-11 19:39  小鲨鱼2018  阅读(1472)  评论(0编辑  收藏  举报