已知字符串str="skdaskerkjsalkj",请统计该字符串中各字母出现的次数
str1="skdaskerkjsalkj"
str2=set(str1)
for i in str2:
sum0=0
for t in str1:
if t==i:
sum0+=1
print('字母:',i,' ','出现次数:',sum0)
哎呀,大意了,还以为是求重复次数最多的字符呢。重写一个。
>>> s = "skdaskerkjsalkj"
>>> for ch, n in zip(s, map(s.count, s)):
print(ch, n)
s 3
k 4
d 1
a 2
s 3
k 4
e 1
r 1
k 4
j 2
s 3
a 2
l 1
k 4
j 2
from collections import Counter
str="skdaskerkjsalkj"
print(dict(Counter(str)))