python 查看字符串出现的次数

如下输出的是个list类型

str = 'abbcccdddd'
b = list(str)
for i in set(b):
    if b.count(i)>=1:
        print('%s出现的次数%d'%(i,b.count(i)))

如下  输出的是个dict类型
from collections import Counter
a = Counter(str)
print(a)

----------------------------------------------

以空格分割字串

str = 'abbcccdddd'

str_list = ' '.join(str)

以空格把字符串拼接成list类型

list = str_list.split(' ')

猜你喜欢

转载自blog.csdn.net/Doraemon_meow_meow/article/details/89023267